package com.hskn.hss.module.tlfacilities.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.alarmsheshi.entity.AlarmSheshi;
import com.hskn.hss.module.alarmsheshi.mapper.AlarmSheshiMapper;
import com.hskn.hss.module.carviolation.entity.CarViolation;
import com.hskn.hss.module.carviolation.mapper.CarViolationMapper;
import com.hskn.hss.module.coverexp.entity.TlCoverExp;
import com.hskn.hss.module.coverexp.mapper.CoverExpMapper;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.dlbjviolation.mapper.DlbjViolationMapper;
import com.hskn.hss.module.elementtotal.vo.ElementsListByAppVo;
import com.hskn.hss.module.elementtotal.vo.ElementsVo;
import com.hskn.hss.module.humancamer.entity.HumanCamer;
import com.hskn.hss.module.humancamer.mapper.HumanCamerMapper;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.mapper.TlCarMapper;
import com.hskn.hss.module.tlcar.service.ITlCarService;
import com.hskn.hss.module.tlcartype.entity.TlCarType;
import com.hskn.hss.module.tlcartype.mapper.TlCarTypeMapper;
import com.hskn.hss.module.tlemployee.service.ITlEmployeeService;
import com.hskn.hss.module.tlemployee.service.impl.TlEmployeeServiceImpl;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;

import com.hskn.hss.module.tlfacilities.to.TIFacilitiesExcelWriteTo;
import com.hskn.hss.module.tlfacilities.vo.*;
import com.hskn.hss.module.tlopetation.entity.TlOperation;
import com.hskn.hss.module.tlopetation.mapper.TlOperationMapper;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.mapper.TlWorkTypeMapper;
import com.hskn.hss.utils.Tools;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.ExcelReaderDataException;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.carTrack.entity.CarTrack;
import com.hskn.hss.module.carTrack.mapper.CarTrackMapper;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.service.IGridService;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.rptCarWork.mapper.RptCarWorkMapper;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.rptEmployeeWork.mapper.RptEmployeeWorkMapper;
import com.hskn.hss.module.tlancienttreeexp.entity.TlAncienttreeExp;
import com.hskn.hss.module.tlancienttreeexp.mapper.TlAncienttreeExpMapper;
import com.hskn.hss.module.tlchannel.entity.TlChannel;
import com.hskn.hss.module.tlchannel.mapper.TlChannelMapper;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlequip.entity.TlEquip;
import com.hskn.hss.module.tlequipbind.entity.TlEquipBind;
import com.hskn.hss.module.tlequipbind.mapper.TlEquipBindMapper;
import com.hskn.hss.module.tlfacilities.entity.TlFacilities;
import com.hskn.hss.module.tlfacilities.mapper.TlFacilitiesMapper;
import com.hskn.hss.module.tlfacilities.service.ITlFacilitiesService;
import com.hskn.hss.module.tlfacilities.to.TIFacilitiesTo;
import com.hskn.hss.module.tlfacilitietype.entity.TlFacilitiesType;
import com.hskn.hss.module.tlfacilitietype.mapper.TlFacilitiesTypeMapper;
import com.hskn.hss.module.tlfactypeitem.entity.TlFacilitiesTypeItems;
import com.hskn.hss.module.tlfactypeitem.mapper.TlFacilitiesTypeItemsMapper;
import com.hskn.hss.module.tlparkexp.entity.TlParkExp;
import com.hskn.hss.module.tlparkexp.mapper.TlParkExpMapper;
import com.hskn.hss.module.tlparkexp.service.ITlParkExpService;
import com.hskn.hss.module.tlparktreeexp.entity.TlParkTreeExp;
import com.hskn.hss.module.tlparktreeexp.mapper.TlParkTreeExpMapper;
import com.hskn.hss.module.tlthreshold.entity.TlThreshold;
import com.hskn.hss.module.tlthreshold.mapper.TlThresholdMapper;
import com.hskn.hss.module.tltoiletexp.entity.TlToiletExp;
import com.hskn.hss.module.tltoiletexp.mapper.TlToiletExpMapper;
import com.hskn.hss.module.tltranstationexp.entity.TlTranstationExp;
import com.hskn.hss.module.tltranstationexp.mapper.TlTranstationExpMapper;
import com.hskn.hss.module.tltrashexp.entity.TlTrashExp;
import com.hskn.hss.module.tltrashexp.mapper.TlTrashExpMapper;
import com.hskn.hss.module.tltreespec.entity.TlTreespec;
import com.hskn.hss.module.tltreespec.mapper.TlTreespecMapper;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.user.service.IUserService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-13
 */
@Service
@Transactional(rollbackFor = {Exception.class, Error.class}) //添加事务
public class TlFacilitiesServiceImpl extends ServiceImpl<TlFacilitiesMapper, TlFacilities> implements ITlFacilitiesService {
    @Resource
    TlFacilitiesMapper tlFacilitiesMapper;
    @Resource
    TlFacilitiesTypeMapper facilitiesTypeMapper;
    @Resource
    TlFacilitiesTypeItemsMapper facilitiesTypeItemsMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;
    @Resource
    TlTranstationExpMapper tlTranstationExpMapper;//中转站 处理站
    @Resource
    TlToiletExpMapper tlToiletExpMapper;//卫生间
    @Autowired
    ITlParkExpService tlParkExpService;
    @Autowired
    ITlFacilitiesService tlFacilitiesService;//园林绿地
    @Resource
    TlParkTreeExpMapper parkTreeExpMapper;//园林绿地
    @Resource
    TlParkExpMapper parkExpMapper;//园林绿地
    @Resource
    TlTrashExpMapper tlTrashExpMapper;//垃圾桶
    @Resource
    TlAncienttreeExpMapper tlAncienttreeExpMapper; //名木古树
    @Resource
    TlThresholdMapper tlThresholdMapper;
    @Resource
    SysDepartMapper sysDepartMapper;
    @Resource
    TlEquipBindMapper tlEquipBindMapper;
    @Resource
    TlChannelMapper tlChannelMapper;
    @Resource
    TlTreespecMapper treespecMapper;
    @Resource
    RptCarWorkMapper rptCarWorkMapper;
    @Resource
    CarTrackMapper carTrackMapper;
    @Resource
    TlEmployeeMapper employeeMapper;
    @Resource
    RptEmployeeWorkMapper employeeWorkMapper;
    @Resource
    UserMapper userMapper;
    @Autowired
    IUserService userService;
    @Autowired
    IGridService iGridService;
    @Resource
    TlFacilitiesTypeMapper tlFacilitiesTypeMapper;
    @Resource
    TlFacilitiesTypeItemsMapper tlFacilitiesTypeItemsMapper;
    @Resource
    private SysDepartMapper departMapper;
    @Autowired
    private ISysDepartService departService;
    @Resource
    private TlParkExpMapper tlParkExpMapper;
    @Resource
    private TlParkTreeExpMapper tlParkTreeExpMapper;
    @Resource
    private TlEquipMapper tlEquipMapper;
    @Resource
    private HumanCamerMapper humanCamerMapper;
    @Resource
    private AlarmSheshiMapper alarmSheshiMapper;
    @Resource
    private RptEmployeeWorkMapper rptEmployeeWorkMapper;
    @Resource
    private AlarmPersonMapper alarmPersonMapper;
    @Resource
    private DlbjViolationMapper dlbjViolationMapper;
    @Resource
    private TlWorkTypeMapper tlWorkTypeMapper;
    @Resource
    private TlEmployeeMapper tlEmployeeMapper;
    @Resource
    private AlarmCarMapper alarmCarMapper;
    @Resource
    private CarViolationMapper carViolationMapper;
    @Resource
    private TlCarMapper tlCarMapper;
    @Resource
    private TlCarTypeMapper tlCarTypeMapper;
    @Resource
    private CoverExpMapper coverExpMapper;
    @Resource
    private TlOperationMapper tlOperationMapper;
    @Autowired
    private ITlCarService tlCarService;
    @Autowired
    private ITlEmployeeService tlEmployeeService;


    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class}) //添加事务
    public AjaxResult insert(TlFacilities tlFacilities) throws Exception {
        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        SysUserDepart sysUserDepart = sysUserDepartMapper.selectOne(queryWrapper);
        //插入部门id
        if (null != sysUserDepart) {
            tlFacilities.setDeptid(sysUserDepart.getDepid());
        }
        tlFacilities.setCreateby(uid);
        tlFacilities.setCreatetime(new Date());
        tlFacilitiesMapper.insert(tlFacilities);

        List<JSONObject> detailsList = tlFacilities.getDetails();
        if (!CollectionUtils.isEmpty(detailsList) && !CollectionUtils.isEmpty(detailsList.get(0))) {
            JSONObject detail = detailsList.get(0);
            if (tlFacilities.getFacltypeid().equals("fb1603e2e9b2fdc643baa7519640bad8") || tlFacilities.getFacltypeid().equals("d386bbf57a47aae71af83ff35d8d0334")) { //中转站 处理站
                TlTranstationExp tlTranstationExp = JSONObject.toJavaObject(detail, TlTranstationExp.class);
                tlTranstationExp.setMaintbid(tlFacilities.getId());
                tlTranstationExp.setMaintypename(tlFacilities.getFaclname());
                tlTranstationExpMapper.insert(tlTranstationExp);
            } else if (tlFacilities.getFacltypeid().equals("bf83a05e10e3d47cf24d2b304c29ac98")) {//公厕
                TlToiletExp tlToiletExp = JSONObject.toJavaObject(detail, TlToiletExp.class);
                tlToiletExp.setMaintbid(tlFacilities.getId());
                tlToiletExpMapper.insert(tlToiletExp);
            } else if (tlFacilities.getFacltypeid().equals("d5e15fd8654ef6071b225b76db6958ad")) {//垃圾桶
                TlTrashExp tlTrashExp = JSONObject.toJavaObject(detail, TlTrashExp.class);
                tlTrashExp.setMaintbid(tlFacilities.getId());
                tlTrashExpMapper.insert(tlTrashExp);
            } else if (tlFacilities.getFacltypeid().equals("ad120d17c4fd2bb5d88bb48cb39e4f38")) {//园林绿地
                TlParkExp tlParkExp = JSONObject.toJavaObject(detail, TlParkExp.class);
                tlParkExp.setMaintbid(tlFacilities.getId());
                tlParkExpService.insert(tlParkExp);
            } else if (tlFacilities.getFacltypeid().equals("c9fb46f67c21cf0a9068260a5911b303")) {//名木古树
                TlAncienttreeExp tlAncienttreeExp = JSONObject.toJavaObject(detail, TlAncienttreeExp.class);
                tlAncienttreeExp.setMaintbid(tlFacilities.getId());
                tlAncienttreeExpMapper.insert(tlAncienttreeExp);
            }
            if (tlFacilities.getFacltypeid().equals("f7ebd0d00cb970397c52cd84fe7a207a")) {//窨井盖
                //写入自检次数
                TlCoverExp tlCoverExpmain = new TlCoverExp();
                tlCoverExpmain.setInsType(tlFacilities.getInsType());
                tlCoverExpmain.setIns(tlFacilities.getIns());
                tlCoverExpmain.setMaintbid(tlFacilities.getId());
                coverExpMapper.insert(tlCoverExpmain);
                //写入提醒周期
                if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(detailsList)){
                    for (JSONObject tl : detailsList) {
                        //提醒时间的判断
                        TlCoverExp tlCoverExp = JSONObject.toJavaObject(tl, TlCoverExp.class);
                        tlCoverExp.setMaintbid(tlFacilities.getId());
                        if (StringUtils.isNotEmpty(tlCoverExp.getRemindTimeWeek())) {
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");//注意月份是MM
                            tlCoverExp.setRemindTime(simpleDateFormat.parse(tlCoverExp.getRemindTimeWeek()));
                        }
                        coverExpMapper.insert(tlCoverExp);
                    }
                }
            }

            if (tlFacilities.getFacltypeid().equals("ad120d17c4fd2bb5d88bb48cb39e4f38") && tlFacilities.getFacltypeid().equals("c9fb46f67c21cf0a9068260a5911b303")) {
                QueryWrapper<TlThreshold> thresholdQueryWrapper = new QueryWrapper<>();
                thresholdQueryWrapper.lambda().eq(TlThreshold::getThresholdtype, "tree");
                thresholdQueryWrapper.lambda().eq(TlThreshold::getFaclitypeid, tlFacilities.getFacltypeid());
                thresholdQueryWrapper.lambda().eq(TlThreshold::getStatus, "1");
                List<TlThreshold> tlThresholdList = tlThresholdMapper.selectList(thresholdQueryWrapper);
                if (!tlThresholdList.isEmpty()) {
                    for (TlThreshold items : tlThresholdList) {
                        items.setId(StringUtils.uuid());
                        items.setItemid(tlFacilities.getId());
                        items.setFaclitypeid(tlFacilities.getFacltypeid());
                        items.setThresholdtype("item");
                        items.setCreatetime(Calendar.getInstance().getTime());
                        items.setUpdateby(uid);
                        tlThresholdMapper.insert(items);
                    }
                }
            } else {
                QueryWrapper<TlThreshold> thresholdQueryWrapper = new QueryWrapper<>();
                thresholdQueryWrapper.lambda().eq(TlThreshold::getThresholdtype, "facl");
                thresholdQueryWrapper.lambda().eq(TlThreshold::getFaclitypeid, tlFacilities.getFacltypeid());
                thresholdQueryWrapper.lambda().eq(TlThreshold::getStatus, "1");
                List<TlThreshold> tlThresholdList = tlThresholdMapper.selectList(thresholdQueryWrapper);
                if (!tlThresholdList.isEmpty()) {
                    for (TlThreshold items : tlThresholdList) {
                        items.setId(StringUtils.uuid());
                        items.setItemid(tlFacilities.getId());
                        items.setFaclitypeid(tlFacilities.getFacltypeid());
                        items.setThresholdtype("item");
                        items.setCreatetime(Calendar.getInstance().getTime());
                        items.setUpdateby(uid);
                        tlThresholdMapper.insert(items);
                    }
                }
            }

        }
        return AjaxResult.success("操作成功");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class}) //添加事务
    public AjaxResult updatebyids(TlFacilities tlFacilities) throws HssException {
        String uid = LoginUtils.getUser().getUid();
        String ids = tlFacilities.getIds();
        String[] idArray = ids.split(",");
        tlFacilities.setUpdateby(uid);
        tlFacilities.setUpdatetime(new Date());
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getId, idArray);
        tlFacilitiesMapper.update(tlFacilities, tlFacilitiesQueryWrapper);

        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectBatchIds(Arrays.asList(idArray));
        List<String> maintbidlist = new ArrayList();
        if (null != tlFacilitiesList && !tlFacilitiesList.isEmpty()) {
            for (TlFacilities tlFacilitie : tlFacilitiesList) {
                maintbidlist.add(tlFacilitie.getId());
            }
        }

        List<JSONObject> detailsList = tlFacilities.getDetails();
        JSONObject detail = null;

        if (!CollectionUtils.isEmpty(detailsList) && !CollectionUtils.isEmpty(detailsList.get(0))) {
            detail = detailsList.get(0);
        }

        for (TlFacilities ttlFacilities :
                tlFacilitiesList) {

            if (ttlFacilities.getFacltypeid().equals("fb1603e2e9b2fdc643baa7519640bad8") || ttlFacilities.getFacltypeid().equals("d386bbf57a47aae71af83ff35d8d0334")) { //中转站 处理站
//                QueryWrapper<TlTranstationExp> tlTranstationExpQueryWrapper = new QueryWrapper<>();
//                tlTranstationExpQueryWrapper.lambda().in(TlTranstationExp::getMaintbid, maintbidlist);
//                tlTranstationExpMapper.delete(tlTranstationExpQueryWrapper);   //中转站 处理站
//                TlTranstationExp tlTranstationExp = JSONObject.toJavaObject(detail, TlTranstationExp.class);
////                for (String maintbid:
////                maintbidlist) {
////                    tlTranstationExp.setMaintbid(maintbid);
////                    tlTranstationExp.setMaintypename(ttlFacilities.getFaclname());
////                    tlTranstationExpMapper.insert(tlTranstationExp);
////                }

            } else if (ttlFacilities.getFacltypeid().equals("bf83a05e10e3d47cf24d2b304c29ac98")) {//公厕
//                QueryWrapper<TlToiletExp> tlToiletExpQueryWrapper = new QueryWrapper<>();
//                tlToiletExpQueryWrapper.lambda().in(TlToiletExp::getMaintbid, maintbidlist);
//                tlToiletExpMapper.delete(tlToiletExpQueryWrapper);
//                TlToiletExp tlToiletExp = JSONObject.toJavaObject(detail, TlToiletExp.class);
////                for (String maintbid:
////                        maintbidlist) {
////                    tlToiletExp.setMaintbid(maintbid);
////                    tlToiletExpMapper.insert(tlToiletExp);
////                }
            } else if (ttlFacilities.getFacltypeid().equals("d5e15fd8654ef6071b225b76db6958ad")) {//垃圾桶
//                QueryWrapper<TlTrashExp> tlTrashExpQueryWrapper = new QueryWrapper<>();
//                tlTrashExpQueryWrapper.lambda().in(TlTrashExp::getMaintbid, maintbidlist);
//                tlTrashExpMapper.delete(tlTrashExpQueryWrapper);
//                TlTrashExp tlTrashExp = JSONObject.toJavaObject(detail, TlTrashExp.class);
//                for (String maintbid:
//                        maintbidlist) {
//                    tlTrashExp.setMaintbid(maintbid);
//                    tlTrashExpMapper.insert(tlTrashExp);
//                }
            } else if (ttlFacilities.getFacltypeid().equals("ad120d17c4fd2bb5d88bb48cb39e4f38")) {//园林绿地
                QueryWrapper<TlParkExp> tlParkExpQueryWrapper = new QueryWrapper<>();
                tlParkExpQueryWrapper.lambda().in(TlParkExp::getMaintbid, maintbidlist);
                QueryWrapper<TlParkExp> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().in(TlParkExp::getMaintbid, maintbidlist);
                tlParkExpMapper.delete(tlParkExpQueryWrapper);
                List<TlParkExp> tlParkExpList = tlParkExpMapper.selectList(queryWrapper);
                List treemaintbidlist = new ArrayList();
                if (null != tlParkExpList && !tlParkExpList.isEmpty()) {
                    for (TlFacilities tlFacilitie : tlFacilitiesList) {
                        treemaintbidlist.add(tlFacilitie.getId());
                    }
                    QueryWrapper<TlParkTreeExp> tlParkTreeExpQueryWrapper = new QueryWrapper<>();
                    tlParkTreeExpQueryWrapper.lambda().in(TlParkTreeExp::getMaintbid, treemaintbidlist);
                    tlParkTreeExpMapper.delete(tlParkTreeExpQueryWrapper);
                }
                TlParkExp tlParkExp = JSONObject.toJavaObject(detail, TlParkExp.class);
                if (Objects.nonNull(tlParkExp)) {
                    for (String maintbid :
                            maintbidlist) {
                        tlParkExp.setMaintbid(maintbid);
                        tlParkExpService.insert(tlParkExp);
                    }
                }

            } else if (ttlFacilities.getFacltypeid().equals("c9fb46f67c21cf0a9068260a5911b303")) {//名木古树
                QueryWrapper<TlAncienttreeExp> tlAncienttreeExpQueryWrapper = new QueryWrapper<>();
                tlAncienttreeExpQueryWrapper.lambda().in(TlAncienttreeExp::getMaintbid, maintbidlist);
                tlAncienttreeExpMapper.delete(tlAncienttreeExpQueryWrapper);
                TlAncienttreeExp tlAncienttreeExp = JSONObject.toJavaObject(detail, TlAncienttreeExp.class);
                if (Objects.nonNull(tlAncienttreeExp)) {
                    for (String maintbid :
                            maintbidlist) {
                        tlAncienttreeExp.setMaintbid(maintbid);
                        tlAncienttreeExpMapper.insert(tlAncienttreeExp);
                    }
                }
            } else if (ttlFacilities.getFacltypeid().equals("f7ebd0d00cb970397c52cd84fe7a207a")) {//窨井盖
                QueryWrapper<TlCoverExp> coverExpQueryWrapper = new QueryWrapper<>();
                coverExpQueryWrapper.lambda().in(TlCoverExp::getMaintbid, maintbidlist);
                coverExpMapper.delete(coverExpQueryWrapper);
                //写入自检次数
                TlCoverExp tlCoverExpmain = new TlCoverExp();
                tlCoverExpmain.setInsType(tlFacilities.getInsType());
                tlCoverExpmain.setIns(tlFacilities.getIns());
                tlCoverExpmain.setMaintbid(maintbidlist.get(0));
                coverExpMapper.insert(tlCoverExpmain);
                //写入提醒周期
                detailsList.forEach(t -> {
                    TlCoverExp tlCoverExp = JSONObject.toJavaObject(t, TlCoverExp.class);
                    tlCoverExp.setMaintbid(ttlFacilities.getId());
                    if (StringUtils.isNotEmpty(tlCoverExp.getRemindTimeWeek())) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");//注意月份是MM
                        try {
                            tlCoverExp.setRemindTime(simpleDateFormat.parse(tlCoverExp.getRemindTimeWeek()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    coverExpMapper.insert(tlCoverExp);
                });
            }
        }
        return AjaxResult.success("操作成功");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class}) //添加事务
    public AjaxResult update(JSONObject params) throws Exception {
        String uid = LoginUtils.getUser().getUid();
        TlFacilities tlFacilities = JSON.parseObject(params.getString("data"), new TypeReference<TlFacilities>() {
        });
        tlFacilities.setUpdateby(uid);
        tlFacilities.setUpdatetime(new Date());
        tlFacilitiesMapper.updateById(tlFacilities);

        List<JSONObject> detailsList = tlFacilities.getDetails();
        if (!CollectionUtils.isEmpty(detailsList) && !CollectionUtils.isEmpty(detailsList.get(0))) {
            JSONObject detail = detailsList.get(0);

            Map delemap = new HashMap();
            delemap.put("maintbid", tlFacilities.getId());
            if (tlFacilities.getFacltypeid().equals("fb1603e2e9b2fdc643baa7519640bad8") || tlFacilities.getFacltypeid().equals("d386bbf57a47aae71af83ff35d8d0334")) { //中转站 处理站
                tlTranstationExpMapper.deleteByMap(delemap);
                TlTranstationExp tlTranstationExp = JSONObject.toJavaObject(detail, TlTranstationExp.class);
                tlTranstationExp.setMaintbid(tlFacilities.getId());
                tlTranstationExp.setMaintypename(tlFacilities.getFaclname());
                tlTranstationExpMapper.insert(tlTranstationExp);
            } else if (tlFacilities.getFacltypeid().equals("bf83a05e10e3d47cf24d2b304c29ac98")) {//公厕
                tlToiletExpMapper.deleteByMap(delemap);
                TlToiletExp tlToiletExp = JSONObject.toJavaObject(detail, TlToiletExp.class);
                tlToiletExp.setMaintbid(tlFacilities.getId());
                tlToiletExpMapper.insert(tlToiletExp);
            } else if (tlFacilities.getFacltypeid().equals("d5e15fd8654ef6071b225b76db6958ad")) {//垃圾桶
                tlTrashExpMapper.deleteByMap(delemap);
                TlTrashExp tlTrashExp = JSONObject.toJavaObject(detail, TlTrashExp.class);
                tlTrashExp.setMaintbid(tlFacilities.getId());
                tlTrashExpMapper.insert(tlTrashExp);
            } else if (tlFacilities.getFacltypeid().equals("ad120d17c4fd2bb5d88bb48cb39e4f38")) {//园林绿地
                tlParkExpService.deletebymaintbid(tlFacilities.getId());
                TlParkExp tlParkExp = JSONObject.toJavaObject(detail, TlParkExp.class);
                tlParkExp.setMaintbid(tlFacilities.getId());
                tlParkExpService.insert(tlParkExp);
            } else if (tlFacilities.getFacltypeid().equals("c9fb46f67c21cf0a9068260a5911b303")) {//名木古树
                tlAncienttreeExpMapper.deleteByMap(delemap);
                TlAncienttreeExp tlAncienttreeExp = JSONObject.toJavaObject(detail, TlAncienttreeExp.class);
                tlAncienttreeExp.setMaintbid(tlFacilities.getId());
                tlAncienttreeExpMapper.insert(tlAncienttreeExp);
            }
        }
        return AjaxResult.success("操作成功");
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class}) //添加事务
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlFacilities tlFacilities = tlFacilitiesMapper.selectById(id);
        tlFacilitiesMapper.deleteById(id);
        Map delemap = new HashMap();
        delemap.put("maintbid", id);
        Map thremap = new HashMap();
        thremap.put("itemid", tlFacilities.getId());
        tlThresholdMapper.deleteByMap(thremap); //报警参数
        if (tlFacilities.getFacltypeid().equals("fb1603e2e9b2fdc643baa7519640bad8") || tlFacilities.getFacltypeid().equals("d386bbf57a47aae71af83ff35d8d0334")) { //中转站 处理站
            tlTranstationExpMapper.deleteByMap(delemap);   //中转站 处理站
        } else if (tlFacilities.getFacltypeid().equals("bf83a05e10e3d47cf24d2b304c29ac98")) {//公厕
            tlToiletExpMapper.deleteByMap(delemap);    //卫生间
        } else if (tlFacilities.getFacltypeid().equals("d5e15fd8654ef6071b225b76db6958ad")) {//垃圾桶
            tlTrashExpMapper.deleteByMap(delemap);   //垃圾桶
        } else if (tlFacilities.getFacltypeid().equals("ad120d17c4fd2bb5d88bb48cb39e4f38")) {//园林绿地
            tlParkExpService.deletebymaintbid(id);   //园林绿地
        } else if (tlFacilities.getFacltypeid().equals("c9fb46f67c21cf0a9068260a5911b303")) {//名木古树
            tlAncienttreeExpMapper.deleteByMap(delemap);
        }
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebatchbyid(TlFacilities tlFacilities) throws HssException {
        String ids = tlFacilities.getIds();
        String[] idArray = ids.split(",");
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectBatchIds(Arrays.asList(idArray));
        tlFacilitiesMapper.deleteBatchIds(Arrays.asList(idArray));

        List maintbidlist = new ArrayList();
        if (null != tlFacilitiesList && !tlFacilitiesList.isEmpty()) {
            for (TlFacilities tlFacilitie : tlFacilitiesList) {
                maintbidlist.add(tlFacilitie.getId());
            }

            QueryWrapper<TlThreshold> tlThresholdQueryWrapper = new QueryWrapper<>();
            tlThresholdQueryWrapper.lambda().in(TlThreshold::getItemid, maintbidlist);
            tlThresholdMapper.delete(tlThresholdQueryWrapper); //报警参数
        }
        for (TlFacilities ttlFacilities :
                tlFacilitiesList) {

            if (ttlFacilities.getFacltypeid().equals("fb1603e2e9b2fdc643baa7519640bad8") || ttlFacilities.getFacltypeid().equals("d386bbf57a47aae71af83ff35d8d0334")) { //中转站 处理站
                QueryWrapper<TlTranstationExp> tlTranstationExpQueryWrapper = new QueryWrapper<>();
                tlTranstationExpQueryWrapper.lambda().in(TlTranstationExp::getMaintbid, maintbidlist);
                tlTranstationExpMapper.delete(tlTranstationExpQueryWrapper);   //中转站 处理站
            } else if (ttlFacilities.getFacltypeid().equals("bf83a05e10e3d47cf24d2b304c29ac98")) {//公厕
                QueryWrapper<TlToiletExp> tlToiletExpQueryWrapper = new QueryWrapper<>();
                tlToiletExpQueryWrapper.lambda().in(TlToiletExp::getMaintbid, maintbidlist);
                tlToiletExpMapper.delete(tlToiletExpQueryWrapper);    //卫生间
            } else if (ttlFacilities.getFacltypeid().equals("d5e15fd8654ef6071b225b76db6958ad")) {//垃圾桶
                QueryWrapper<TlTrashExp> tlTrashExpQueryWrapper = new QueryWrapper<>();
                tlTrashExpQueryWrapper.lambda().in(TlTrashExp::getMaintbid, maintbidlist);
                tlTrashExpMapper.delete(tlTrashExpQueryWrapper);   //垃圾桶
            } else if (ttlFacilities.getFacltypeid().equals("ad120d17c4fd2bb5d88bb48cb39e4f38")) {//园林绿地
                QueryWrapper<TlParkExp> tlParkExpQueryWrapper = new QueryWrapper<>();
                tlParkExpQueryWrapper.lambda().in(TlParkExp::getMaintbid, maintbidlist);
                QueryWrapper<TlParkExp> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().in(TlParkExp::getMaintbid, maintbidlist);
                tlParkExpMapper.delete(tlParkExpQueryWrapper);
                List<TlParkExp> tlParkExpList = tlParkExpMapper.selectList(queryWrapper);
                List treemaintbidlist = new ArrayList();
                if (null != tlParkExpList && !tlParkExpList.isEmpty()) {
                    for (TlFacilities tlFacilitie : tlFacilitiesList) {
                        treemaintbidlist.add(tlFacilitie.getId());
                    }
                    QueryWrapper<TlParkTreeExp> tlParkTreeExpQueryWrapper = new QueryWrapper<>();
                    tlParkTreeExpQueryWrapper.lambda().in(TlParkTreeExp::getMaintbid, treemaintbidlist);
                    tlParkTreeExpMapper.delete(tlParkTreeExpQueryWrapper);
                }

            } else if (ttlFacilities.getFacltypeid().equals("c9fb46f67c21cf0a9068260a5911b303")) {//名木古树
                QueryWrapper<TlAncienttreeExp> tlAncienttreeExpQueryWrapper = new QueryWrapper<>();
                tlAncienttreeExpQueryWrapper.lambda().in(TlAncienttreeExp::getMaintbid, maintbidlist);
                tlAncienttreeExpMapper.delete(tlAncienttreeExpQueryWrapper);
            } else if (ttlFacilities.getFacltypeid().equals("f7ebd0d00cb970397c52cd84fe7a207a")) {//窨井盖
                QueryWrapper<TlCoverExp> coverExpQueryWrapper = new QueryWrapper<>();
                coverExpQueryWrapper.lambda().in(TlCoverExp::getMaintbid, maintbidlist);
                coverExpMapper.delete(coverExpQueryWrapper);
            }

        }
        return AjaxResult.success("操作成功");
    }

    @Override
    public List<RptCarWork> selectLjpyWorkPlan(String fid) {
        StringBuilder str = new StringBuilder();
        QueryWrapper<RptCarWork> ljqyWorkPlanQueryWrapper = new QueryWrapper<>();
        ljqyWorkPlanQueryWrapper.like("sjd_ids", fid);
        ljqyWorkPlanQueryWrapper.last("and  DATE_FORMAT(plan_start_time,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')");
        List<RptCarWork> rptCarWorks = rptCarWorkMapper.selectList(ljqyWorkPlanQueryWrapper);

        if (!rptCarWorks.isEmpty()) {
            str.append(" and");
        }
        for (int i = 0; i < rptCarWorks.size(); i++) {
            str.append(" ( DATE_FORMAT('" + Tools.getDateString(rptCarWorks.get(i).getPlanStartTime()) + "','%Y-%m-%d %H:%i:%s') < DATE_FORMAT( create_time, '%Y-%m-%d %H:%i:%s' )" +
                    " and  DATE_FORMAT('" + Tools.getDateString(rptCarWorks.get(i).getPlanEndTime()) + "','%Y-%m-%d %H:%i:%s') > DATE_FORMAT( create_time, '%Y-%m-%d %H:%i:%s' )) or");
        }
        String sql = null;

        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.like("facility_id", fid);
        if (!rptCarWorks.isEmpty()) {
            sql = str.substring(0, str.length() - 2);
            carTrackQueryWrapper.last(sql);
        }
        List<CarTrack> carTrackList = carTrackMapper.selectList(carTrackQueryWrapper);
        for (int i = 0; i < carTrackList.size(); i++) {
            for (int j = 0; j < rptCarWorks.size(); j++) {
                if (rptCarWorks.get(j).getPlanStartTime() == null
                        || rptCarWorks.get(j).getPlanEndTime() == null
                        || carTrackList.get(i).getCreateTime() == null) {
                    break;
                }
                if (rptCarWorks.get(j).getPlanStartTime().getTime() <= carTrackList.get(i).getCreateTime().getTime()
                        && carTrackList.get(i).getCreateTime().getTime() <= rptCarWorks.get(j).getPlanEndTime().getTime()) {
                    rptCarWorks.get(j).setQlFlag(1);
                } else {
                    rptCarWorks.get(j).setQlFlag(0);
                }

            }

        }

        return rptCarWorks;
    }

    @Override
    public TlFacilities getDetail(TlFacilities tlFacilities) throws HssException, ParseException {
        String id = tlFacilities.getId();
        Map<String, Grid> gridCollect = iGridService.list().stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));

        TlFacilities ttlFacilities = tlFacilitiesMapper.selectOneById(id);
        User user = userMapper.selectById(ttlFacilities.getPrincipal());
        if (user != null) {
            ttlFacilities.setPrincipalname(user.getName());
            ttlFacilities.setPhone(user.getPhone());
        }
        TlFacilitiesType facilitiesType = facilitiesTypeMapper.selectById(ttlFacilities.getFacltypeid());
        TlFacilitiesTypeItems facilitiesTypeItems = facilitiesTypeItemsMapper.selectById(ttlFacilities.getFacltypeitemid());
        ttlFacilities.setFacltypename(facilitiesType.getFacilitietypename());
        ttlFacilities.setFacltypeitemname(facilitiesTypeItems.getItemtypename());
        if (null != gridCollect.get(ttlFacilities.getGridId())) {
            ttlFacilities.setGridname(gridCollect.get(ttlFacilities.getGridId()).getName());
        }
        String equipid = ttlFacilities.getEquipid();
        if (!StringUtils.isEmpty(equipid)) {
            QueryWrapper<TlChannel> tlChannelQueryWrapper = new QueryWrapper<>();
            tlChannelQueryWrapper.lambda().eq(TlChannel::getEquipid, equipid);
            tlChannelQueryWrapper.lambda().orderByAsc(TlChannel::getChannelname);
            List details = tlChannelMapper.selectList(tlChannelQueryWrapper);
            ttlFacilities.setChannels(details);
        }

        if (!StringUtils.isEmpty(ttlFacilities.getFacldeptid())) {
            SysDepart sysDepart = sysDepartMapper.getDeptIdById(ttlFacilities.getFacldeptid());
            if (null != sysDepart) {
                ttlFacilities.setFacldeptname(sysDepart.getDepartName());
            }
        }
        if (!StringUtils.isEmpty(ttlFacilities.getMtadeptid())) {
            SysDepart mtDepart = sysDepartMapper.getDeptIdById(ttlFacilities.getMtadeptid());
            if (null != mtDepart) {
                ttlFacilities.setMtadeptname(mtDepart.getDepartName());
            }
        }
        List itemsList = new ArrayList();
        List itemsList1 = new ArrayList();
        TlFacilities tlFacilities1 = new TlFacilities();
        Map map = new HashMap();
        map.put("maintbid", tlFacilities.getId());
        if (null != ttlFacilities.getFacltypeid() && !ttlFacilities.getFacltypeid().isEmpty()) {
            if (ttlFacilities.getFacltypeid().equals("fb1603e2e9b2fdc643baa7519640bad8") || ttlFacilities.getFacltypeid().equals("d386bbf57a47aae71af83ff35d8d0334")) { //中转站 处理站
                itemsList = tlTranstationExpMapper.selectByMap(map);
            } else if (ttlFacilities.getFacltypeid().equals("bf83a05e10e3d47cf24d2b304c29ac98")) {//公厕
                itemsList = tlToiletExpMapper.selectByMap(map);
            } else if (ttlFacilities.getFacltypeid().equals("d5e15fd8654ef6071b225b76db6958ad")) {//垃圾桶
                itemsList = tlTrashExpMapper.selectByMap(map);
            } else if (ttlFacilities.getFacltypeid().equals("ad120d17c4fd2bb5d88bb48cb39e4f38")) {//园林绿地
                itemsList = tlParkExpService.getDetail(tlFacilities.getId());
            } else if (ttlFacilities.getFacltypeid().equals("c9fb46f67c21cf0a9068260a5911b303")) {//名木古树
                itemsList = tlAncienttreeExpMapper.selectByMap(map);
            } else if (ttlFacilities.getFacltypeid().equals("f7ebd0d00cb970397c52cd84fe7a207a")) {//窨井盖
                itemsList = coverExpMapper.selectByMap(map);
                if (org.apache.commons.collections.CollectionUtils.isNotEmpty(itemsList)) {
                    List<TlCoverExp> tlFacilitiesList = itemsList;
                    for (TlCoverExp facilitie : tlFacilitiesList) {
                        if (StringUtils.isEmpty(facilitie.getRemindType())) {
                            tlFacilities1.setInsType(facilitie.getInsType());
                            tlFacilities1.setIns(facilitie.getIns());
                        } else {
                            itemsList1.add(facilitie);
                        }
                    }
                    tlFacilities1.setDetails(itemsList1);
                    itemsList.clear();
                    itemsList.add(tlFacilities1);
                }
            }
        }
        ttlFacilities.setDetails(itemsList);
        TIFacilitiesTo tiFacilitiesTo = new TIFacilitiesTo();
        tiFacilitiesTo.setId(tlFacilities.getId());
        ttlFacilities.setToiletPersonVOList(getToiletPerson(tiFacilitiesTo).getFactypelist());
        return ttlFacilities;
    }

    @Override
    public Map<String, Object> getTreeSpecList(String FId) {
        QueryWrapper<TlParkExp> parkExpQueryWrapper = new QueryWrapper<>();
        parkExpQueryWrapper.lambda().eq(TlParkExp::getMaintbid, FId);
        parkExpQueryWrapper.last("limit 1");
        TlParkExp parkExp = parkExpMapper.selectOne(parkExpQueryWrapper);
        if (parkExp == null) {
            return new HashMap<>();
        }
        QueryWrapper<TlParkTreeExp> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TlParkTreeExp::getMaintbid, parkExp.getId());
        queryWrapper.orderByDesc("treenum");
        List<TlParkTreeExp> list = parkTreeExpMapper.selectList(queryWrapper);
        List<BTable> bTables = parkTreeExpMapper.selectTreeAgeDistribution(parkExp.getId());
        List<String> ids = new ArrayList<>();
        List<TlTreespec> treespecList = new ArrayList<>();
        for (TlParkTreeExp t : list) {
            if (StringUtils.isNotEmpty(t.getTreeSeedId())) {
                ids.add(t.getTreeSeedId());
            }
        }
        if (ids.size() > 0) {
            QueryWrapper<TlTreespec> tlTreespecQueryWrapper = new QueryWrapper<>();
            tlTreespecQueryWrapper.in("id", ids);
            treespecList = treespecMapper.selectList(tlTreespecQueryWrapper);
        }
        for (TlParkTreeExp t : list) {
            for (TlTreespec y : treespecList) {
                if (t.getTreeSeedId().equals(y.getId())) {
                    t.setIconid(y.getIconid());
                }
            }
        }
        Map<String, Object> map = new HashMap();

        List<TlParkTreeExp> resList = new ArrayList<>();
        List<TlParkTreeExp> tlParkTreeExps = new ArrayList<>();
        if (!list.isEmpty()) {
            Map<String, List<TlParkTreeExp>> parkTreeExpMap = GroupingByUtils.getGroupingForMapList(list, TlParkTreeExp::getMaintree);
            for (String s : parkTreeExpMap.keySet()) {
                tlParkTreeExps = parkTreeExpMap.get(s).stream().sorted(Comparator.comparing(TlParkTreeExp::getSpecsInteger)).collect(Collectors.toList());
                if (tlParkTreeExps.size() > 1) {
                    tlParkTreeExps.get(0).setSpecsName(tlParkTreeExps.get(0).getSpecsInteger() + "-" + tlParkTreeExps.get(tlParkTreeExps.size() - 1).getSpecsInteger());
                    resList.add(tlParkTreeExps.get(0));
                } else {
                    tlParkTreeExps.get(0).setSpecsName(tlParkTreeExps.get(0).getSpecs());
                    resList.add(parkTreeExpMap.get(s).get(0));
                }
            }
        }
        map.put("treespecList", resList);
        map.put("bTables", bTables);
        if (parkExp != null && parkExp.getTreeallnum() != null && parkExp.getGrowntree() != null && parkExp.getTreeallnum() > 0) {
            map.put("szsNum", new BigDecimal(parkExp.getGrowntree()).divide(new BigDecimal(parkExp.getTreeallnum()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(1, BigDecimal.ROUND_HALF_UP));
        } else {
            map.put("szsNum", 0);
        }
        if (parkExp != null && parkExp.getTreeallnum() != null && parkExp.getHealtree() != null && parkExp.getTreeallnum() > 0) {
            map.put("jkNum", new BigDecimal(parkExp.getHealtree()).divide(new BigDecimal(parkExp.getTreeallnum()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(1, BigDecimal.ROUND_HALF_UP));
        } else {
            map.put("jkNum", 0);
        }
        if (parkExp != null && parkExp.getArea() != null && parkExp.getGreenarea() != null && parkExp.getArea() > 0) {
            map.put("lhlNum", new BigDecimal(parkExp.getGreenarea()).divide(new BigDecimal(parkExp.getArea()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(1, BigDecimal.ROUND_HALF_UP));
        } else {
            map.put("lhlNum", 0);
        }

        return map;
    }

    @Override
    public TlTreespec getTreeSpec(String id) {
        treespecMapper.selectById(id);
        return treespecMapper.selectById(id);
    }

    @Override
    public List getSelectList() {
        List resultList = tlFacilitiesMapper.getSelectList();
        return resultList;
    }

    @Override
    public List getSelectListByuid(JSONObject params) {

        String uid = LoginUtils.getUser().getUid();
        Map map = new HashMap();
        map.put("facltypeid", params.getString("facltypeid"));
        map.put("principal", uid);
        List resultList = tlFacilitiesMapper.selectByMap(map);
        return resultList;
    }

    @Override
    @DataScope(deptAlias = "facldeptid", userAlias = "principal, createby, updateby")
    public TIFacilitiesVO getList(BaseEntity entity, TIFacilitiesTo tiFacilitiesTo) throws HssException {
        Long page = StringUtils.isEmpty(tiFacilitiesTo.getPage()) ? 1 : tiFacilitiesTo.getPage();
        Long size = StringUtils.isEmpty(tiFacilitiesTo.getSize()) ? 10 : tiFacilitiesTo.getSize();
        JSONObject json = new JSONObject();
        String faclname = tiFacilitiesTo.getFaclname();
        List<Object> faclnamelist = new ArrayList<>();
        if (null != faclname && !faclname.equals("")) {
            faclname = "('" + faclname.replace(",", "','") + "')";
        }
        Map<String, Object> map = new HashMap();
        map.put("facltypeid", tiFacilitiesTo.getFacltypeid());
        map.put("facltypeitemid", tiFacilitiesTo.getFacltypeitemid());
        map.put("faclname", faclname);
        map.put("faclstatus", tiFacilitiesTo.getFaclstatus());
        map.put("mtadeptId", tiFacilitiesTo.getMtadeptId());
        map.put("type", tiFacilitiesTo.getType());
        map.put("params", entity.getParams());

        String idsStr = tiFacilitiesTo.getIds();
        if (StringUtils.isNotEmpty(idsStr)) {
            String[] ids = idsStr.split(",");
            if (StringUtils.isNotEmpty(ids) && ids.length > 0) {
                map.put("ids", Arrays.asList(ids));
            }
        }
        String excludeIdsStr = tiFacilitiesTo.getExcludeIds();
        if (StringUtils.isNotEmpty(excludeIdsStr)) {
            String[] excludeIds = excludeIdsStr.split(",");
            if (StringUtils.isNotEmpty(excludeIds) && excludeIds.length > 0) {
                map.put("excludeIds", Arrays.asList(excludeIds));
            }
        }

        Page<TlFacilities> listPage = new Page<>(page, size, true);
        IPage<TlFacilities> iPage = tlFacilitiesMapper.listTlFacilities(listPage, map);
        for (TlFacilities t : iPage.getRecords()) {
            Map param = new HashMap();
            param.put("itemid", t.getId());

            t.setBinds(tlEquipBindMapper.selectByMap(param));
        }
        TIFacilitiesVO tiFacilitiesVO = new TIFacilitiesVO();
        tiFacilitiesVO.setTotal(iPage.getTotal());
        tiFacilitiesVO.setSize(iPage.getSize());
        tiFacilitiesVO.setPage(iPage.getPages());
        tiFacilitiesVO.setCurrent(iPage.getCurrent());
        tiFacilitiesVO.setFactypelist(iPage.getRecords());
        return tiFacilitiesVO;
    }


    @Override
    public List<TreeEntity> getParamTree(JSONObject params) {
        List<TreeEntity> treeList = sysDepartMapper.getTreeList();
        Map<String, Object> map = new HashMap();
        String treetype = params == null ? "" : params.getString("treetype");
        if (treetype != null && !treetype.equals("")) {
            map.put("treetype", treetype);
            List<TreeEntity> paramTree = tlFacilitiesMapper.getParamTree(map);
            treeList.addAll(paramTree);
        }
        List<TreeEntity> trees = TreeUtil.RecursiveAddress(treeList);
        return trees;
    }

    @Override
    public List<JSONObject> selectCarWorkByDate(JSONObject params) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (params == null) {
            params = new JSONObject();
        }
        if (!params.containsKey("date")) {
            Date date = new Date();
            params.put("date", simpleDateFormat.format(date));
        }
        List<JSONObject> rusList = new ArrayList<>();
        String dateString = params.getString("date");
        JSONObject res = null;
        Date date = simpleDateFormat.parse(dateString);
        int num = Tools.getMonthNumber(date).lengthOfMonth();
        String yearMon = Tools.getYearMonthString(date);
        List<JSONObject> list = tlFacilitiesMapper.selectCarWorkByDate(params);
        for (int i = 1; i <= num; i++) {
            res = new JSONObject();
            res.put("date", yearMon + "-" + (i >= 10 ? i : "0" + i));
            res.put("is_later_in", 0);
            res.put("is_no_work", 0);
            res.put("is_leave_early", 0);
            res.put("sjCount", 0);
            rusList.add(res);
        }
        for (int i = 0; i < rusList.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if (rusList.get(i).getString("date").equals(list.get(j).getString("date"))) {
                    rusList.get(i).put("is_later_in", list.get(j).getString("is_later_in"));
                    rusList.get(i).put("is_no_work", list.get(j).getString("is_no_work"));
                    rusList.get(i).put("is_leave_early", list.get(j).getString("is_leave_early"));
                    rusList.get(i).put("sjCount", list.get(j).getString("sjCount"));
                }
            }
        }

        return rusList;
    }

    @Override
    public JSONObject selectTlequipByFacId(JSONObject params) {
        if (params == null) {
            params = new JSONObject();
        }
        JSONObject res = new JSONObject();
        QueryWrapper<TlEquipBind> equipBindQueryWrapper = new QueryWrapper<>();
        TlFacilities facilities = null;
        if (params.containsKey("id")) {
            String id = params.getString("id");
            equipBindQueryWrapper.eq("itemid", id);
            facilities = tlFacilitiesMapper.selectOneById(id);
        }
        User user = new User();
        TlEmployee employee = new TlEmployee();
        RptEmployeeWork employeeWork = new RptEmployeeWork();
        if (facilities != null && StringUtils.isNotEmpty(facilities.getPrincipal())) {
            user = userMapper.selectById(facilities.getPrincipal());
            QueryWrapper<TlEmployee> employeeQueryWrapper = new QueryWrapper<>();
            if (user != null) {
                employeeQueryWrapper.lambda().eq(TlEmployee::getBindaccount, user.getUid());
            }
            employeeQueryWrapper.lambda().last(" LIMIT 1");
            employee = employeeMapper.selectOne(employeeQueryWrapper);
            QueryWrapper<RptEmployeeWork> employeeWorkQueryWrapper = new QueryWrapper<>();
            employeeWorkQueryWrapper.eq("employee_id", facilities.getPrincipal());
            employeeWorkQueryWrapper.orderByDesc("work_start_time");
            employeeWorkQueryWrapper.last("limit 1");
            employeeWork = employeeWorkMapper.selectOne(employeeWorkQueryWrapper);
        }

        List<TlEquipBind> Equips = tlEquipBindMapper.selectList(equipBindQueryWrapper);

        res.put("equips", Equips);
        if (user != null) {
            TlOperation tlOperation = new TlOperation();
            if (employee != null && StringUtils.isNotEmpty(employee.getPersontype())) {
                tlOperation = tlOperationMapper.selectById(employee.getPersontype());
                if (null != tlOperation) {
                    if (StringUtils.isEmpty(tlOperation.getOperationtype())) {
                        tlOperation.setOperationtype("道路保洁");
                    }
                }
            }
            res.put("name", employee.getName());
            res.put("persontype", employee.getPersontype());
            res.put("persontypename", tlOperation.getOperationtype());
        } else {
            res.put("name", "");
            res.put("persontype", "");
            res.put("persontypename", "");
        }
        if (employeeWork != null) {
            res.put("work_start_time", employeeWork.getWorkStartTime());
        } else {
            res.put("work_start_time", "");
        }
        return res;
    }

    @Override
    public Map<String, TlFacilities> getFacMap(QueryWrapper<TlFacilities> queryWrapper) {
        List<TlFacilities> facilitiesList = tlFacilitiesMapper.selectList(queryWrapper);
        Map<String, TlFacilities> facilitiesMap = new HashMap<>();
        if (!facilitiesList.isEmpty()) {
            facilitiesMap = GroupingByUtils.getGroupingForMapEntity(facilitiesList,
                    TlFacilities::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return facilitiesMap;
    }

    @Override    public Map<String, List<TlFacilities>> getFacTypeMap(QueryWrapper<TlFacilities> queryWrapper) {
        List<TlFacilities> facilitiesList = tlFacilitiesMapper.selectList(queryWrapper);
        List<TlFacilities> facilitiesList2 = new ArrayList<>();
        for (TlFacilities tlFacilities : facilitiesList) {
            String faclname = StringUtils.isNotEmpty(tlFacilities.getFacltypename()) ? tlFacilities.getFacltypename() : "" ;
            tlFacilities.setFaclname(faclname);
            facilitiesList2.add(tlFacilities);
        }
        Map<String, List<TlFacilities>> facilitiesMap = new HashMap<>();
        if (!facilitiesList.isEmpty()) {
            facilitiesMap = facilitiesList2.stream().collect(Collectors.groupingBy(TlFacilities::getFacltypename));
        }
        return facilitiesMap;
    }


    @Override
    public void excelReader(List<TlFacilities> tlFacilitiesList) throws ExcelReaderDataException {

        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<TlFacilitiesType> tlFacilitiesTypeQueryWrapper = new QueryWrapper<>();
        List<TlFacilitiesType> tlFacilitiesTypeList = tlFacilitiesTypeMapper.selectList(tlFacilitiesTypeQueryWrapper);
        Map<String, TlFacilitiesType> tlFacilitiesTypeMap = new HashMap<>();
        QueryWrapper<TlFacilitiesTypeItems> tlFacilitiesTypeItemsQueryWrapper = new QueryWrapper<>();
        List<TlFacilitiesTypeItems> tlFacilitiesTypeItemsList = tlFacilitiesTypeItemsMapper.selectList(tlFacilitiesTypeItemsQueryWrapper);
        Map<String, TlFacilitiesTypeItems> tlFacilitiesTypeItemsMap = new HashMap<>();
        List<SysDepart> departList = departMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> departMap = new HashMap<>();
        if (!departList.isEmpty()) {
            Predicate<SysDepart> departNameNotNull = depart -> StringUtils.isNotEmpty(depart.getDepartName());
            departList = departList.stream().filter(departNameNotNull).collect(Collectors.toList());
            departMap = GroupingByUtils.getGroupingForMapEntity(departList, SysDepart::getDepartName, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        if (!tlFacilitiesTypeList.isEmpty()) {
            Predicate<TlFacilitiesType> facilitiesTypeNotNull = facilitiesType -> StringUtils.isNotEmpty(facilitiesType.getFacilitietypename());
            tlFacilitiesTypeList = tlFacilitiesTypeList.stream().filter(facilitiesTypeNotNull).collect(Collectors.toList());
            tlFacilitiesTypeMap = GroupingByUtils.getGroupingForMapEntity(tlFacilitiesTypeList, TlFacilitiesType::getFacilitietypename, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        if (!tlFacilitiesTypeItemsList.isEmpty()) {
            Predicate<TlFacilitiesTypeItems> facilitiesTypeItemNotNull = facilitiesTypeItem -> StringUtils.isNotEmpty(facilitiesTypeItem.getItemtypename());
            tlFacilitiesTypeItemsList = tlFacilitiesTypeItemsList.stream().filter(facilitiesTypeItemNotNull).collect(Collectors.toList());
            tlFacilitiesTypeItemsMap = GroupingByUtils.getGroupingForMapEntity(tlFacilitiesTypeItemsList, TlFacilitiesTypeItems::getItemtypename, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        int num = 1;
        String str = "";

        //导入重复判断

        List<String> code = tlFacilitiesList.stream().map(TlFacilities::getFaclcode).collect(Collectors.toList());
        if (AttributeUtils.RepeatedCheck(code) == false) {
            for (Object a : AttributeUtils.RepeatedList(code)) {
                str += "'第" + a + "条' 的 “设施编号:" + code.get((int) a - 1) + "”  重复;##";
            }
        }

        List<String> name = tlFacilitiesList.stream().map(TlFacilities::getFaclname).collect(Collectors.toList());
        if (AttributeUtils.RepeatedCheck(name) == false) {
            for (Object a : AttributeUtils.RepeatedList(code)) {
                str += "'第" + a + "条' 的 “设施名称:" + code.get((int) a - 1) + "”  重复;##";
            }
        }

        List<String> faciltypename = tlFacilitiesList.stream().map(TlFacilities::getFacltypename).collect(Collectors.toList());
        if (AttributeUtils.RepeatedCheck(faciltypename) == false) {
            for (Object a : AttributeUtils.RepeatedList(code)) {
                str += "'第" + a + "条' 的 “设施大类:" + code.get((int) a - 1) + "”  重复;##";
            }
        }

        List<String> faciltypeitemname = tlFacilitiesList.stream().map(TlFacilities::getFacltypeitemname).collect(Collectors.toList());
        if (AttributeUtils.RepeatedCheck(faciltypeitemname) == false) {
            for (Object a : AttributeUtils.RepeatedList(code)) {
                str += "'第" + a + "条' 的 “设施小类:" + code.get((int) a - 1) + "”  重复;##";
            }

        }
        //导入重复判断结束
        List<TlFacilities> tlFacilities = tlFacilitiesMapper.selectList(new QueryWrapper<>());
        for (TlFacilities facilities : tlFacilitiesList) {
            facilities.setCreatetime(new Date());
            facilities.setCreateby(uid);
            if (tlFacilitiesTypeMap.containsKey(facilities.getFacltypename())) {
                facilities.setFacltypeid(tlFacilitiesTypeMap.get(facilities.getFacltypename()).getId());
            } else {
                str += "第" + num + "条的“‘设施类型（大类）’:‘" + facilities.getFacltypename() + "’”" + "不存在;##";
            }
            if (tlFacilitiesTypeItemsMap.containsKey(facilities.getFacltypeitemname())) {
                facilities.setFacltypeitemid(tlFacilitiesTypeItemsMap.get(facilities.getFacltypeitemname()).getId());
            } else {
                str += "第" + num + "条的“‘设施类型（小类）’:‘" + facilities.getFacltypeitemname() + "’”" + "不存在;##";
            }
            if (departMap.containsKey(facilities.getFacldeptname())) {
                facilities.setFacldeptid(departMap.get(facilities.getFacldeptname()).getId());

            } else {
                str += "第" + num + "条的“‘所属机构’:‘" + facilities.getFacldeptname() + "’”" + "不存在;##";
            }


            //设施ID设施名称为空判断
//            if (StringUtils.isEmpty(facilities.getFaclcode())) {
//                str += "'第" + num + "条' 的 “设施编号” 不能为空;##";
//            }
            if (StringUtils.isEmpty(facilities.getFaclname())) {
                str += "'第" + num + "条' 的 “设施名称” 不能为空;##";
            }
            if (StringUtils.isEmpty(facilities.getFacltypename())) {
                str += "'第" + num + "条' 的 “设施大类” 不能为空;##";
            }
            if (StringUtils.isEmpty(facilities.getFacltypeitemname())) {
                str += "'第" + num + "条' 的 “设施小类” 不能为空;##";
            }
            if (StringUtils.isEmpty(facilities.getFacldeptname())) {
                str += "'第" + num + "条' 的 “所属机构” 不能为空;##";
            }
            if (StringUtils.isEmpty(facilities.getRadii())) {
                str += "'第" + num + "条' 的 “点半径” 不能为空;##";
            }
            //设施ID设施名称为空判断结束

            //数据库查询判断=======================================
            int ck = 0;
            for (TlFacilities al : tlFacilities) {
                if (StringUtils.isNotEmpty(al.getFaclcode()) && facilities.getFaclcode().equals(al.getFaclcode()) && ck == 0) {
                    str += "'第" + num + "条' 档案中已存在 “设施编号:" + facilities.getFaclcode() + "”;";
                    ck = 1;
                }
                if (StringUtils.isNotEmpty(al.getFaclname()) && facilities.getFaclname().equals(al.getFaclname()) && ck < 2) {
                    if (ck > 0) {
                        str += "“设备名:" + facilities.getFaclname() + "”;";
                    } else {
                        str += "'第" + num + "条' 档案中已存在 “设施名称:" + facilities.getFaclname() + "”;";
                        ck = 2;
                    }
                }
            }
            if (ck > 0) {
                str += "##";
            }
            //数据库查询判断结束=======================================
            num++;
        }
        if (StringUtils.isNotEmpty(str)) {
            throw new ExcelReaderDataException("您导入的数据:##" + str);
        }
    }


    @Override
    public TIFacilitiesVO getToiletPerson(TIFacilitiesTo tiFacilitiesTo) throws HssException, ParseException {
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getId())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getId, tiFacilitiesTo.getId());

        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFaclname())) {
            List<String> faclnameList = Arrays.asList(tiFacilitiesTo.getFaclname().split(","));
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFaclname, faclnameList);
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacldeptid())) {
            List<String> depIds = departService.getChildIdsByIId(tiFacilitiesTo.getFacldeptid());
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFacldeptid, depIds);
        }
        tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, "bf83a05e10e3d47cf24d2b304c29ac98");
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
        TIFacilitiesVO tiFacilitiesVO = new TIFacilitiesVO();
        QueryWrapper<TlEquip> tlEquipQueryWrapper = new QueryWrapper<>();
        tlEquipQueryWrapper.lambda().eq(TlEquip::getEquiptypeid, "0affaa7feae8e0a49233b7de67cfaebd");
        List<TlEquip> tlEquipList = tlEquipMapper.selectList(tlEquipQueryWrapper);
        Map<Object, List<TlEquip>> tlEquipCollect = tlEquipList.stream().filter(item -> StringUtils.isNotEmpty(item.getBindobjid())).collect(Collectors.groupingBy(TlEquip::getBindobjid));

        Date startTime;
        Date endTime;
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getStartTime()) && StringUtils.isNotEmpty(tiFacilitiesTo.getEndTime())) {
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(tiFacilitiesTo.getEndTime()));
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(tiFacilitiesTo.getStartTime()));
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            String first = format.format(c.getTime());
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(first));
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = format.format(ca.getTime());
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(last));
        }
        QueryWrapper<HumanCamer> HumanCamerQueryWrapper = new QueryWrapper<>();
        HumanCamerQueryWrapper.lambda().between(HumanCamer::getDeviceTime, startTime, endTime);
        HumanCamerQueryWrapper.lambda().orderByDesc(HumanCamer::getDeviceTime);
        List<HumanCamer> HumanCamerListAll = humanCamerMapper.selectList(HumanCamerQueryWrapper);
        for (HumanCamer humanCamer : HumanCamerListAll) {
            humanCamer.setDeviceDate(Tools.getYearMonthDayString(humanCamer.getDeviceTime()));
        }

        Map<String, List<HumanCamer>> HumanCamerMapByEquipNum = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
        Map<String, List<HumanCamer>> HumanCamerMapByDeviceDate = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getDeviceDate));

        for (TlFacilities tlFacilities : tlFacilitiesList) {

            if (null != tlEquipCollect.get(tlFacilities.getId())) {
                tlFacilities.setEqpnum(tlEquipCollect.get(tlFacilities.getId()).get(0).getEquipcode());
                if (null == tlFacilities.getEqpnum() || null == HumanCamerMapByEquipNum.get(tlFacilities.getEqpnum())) {
                    continue;
                }

                List<HumanCamer> HumanCamerList = HumanCamerMapByEquipNum.get(tlFacilities.getEqpnum());
                tlFacilities.setPersoncountall(HumanCamerList.get(0).getIntotal());

                if (!HumanCamerList.isEmpty()) {
                    if (StringUtils.isNotEmpty(tiFacilitiesTo.getStartTime()) && StringUtils.isNotEmpty(tiFacilitiesTo.getEndTime())) {
                        List<Date> datelist = new ArrayList<>();
                        int percount = 0;
                        datelist = Tools.getBetweenDateList(tiFacilitiesTo.getStartTime(), tiFacilitiesTo.getEndTime());
                        for (Date date : datelist) {

                            String datetian = Tools.getYearMonthDayString(date);
                            List<HumanCamer> HHumanCamerList = HumanCamerMapByDeviceDate.get(datetian);
                            if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                                Map<String, List<HumanCamer>> HumanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                                List<HumanCamer> HHumanCamerListByEqpNum = HumanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                                if (null != HHumanCamerListByEqpNum && HHumanCamerListByEqpNum.size() > 0) {
                                    percount = percount + HHumanCamerListByEqpNum.get(0).getIntoday();

                                }
                            }
                        }
                        tlFacilities.setPersoncount(percount);

                    } else {
                        int percount = 0;
                        List<String> datelists = Tools.getDayListOfMonth();
                        for (String date : datelists) {

                            List<HumanCamer> HHumanCamerList = HumanCamerMapByDeviceDate.get(date);
                            if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                                Map<String, List<HumanCamer>> HumanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                                List<HumanCamer> HHumanCamerListByEqpNum = HumanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                                if (null != HHumanCamerListByEqpNum && HHumanCamerListByEqpNum.size() > 0) {
                                    percount = percount + HHumanCamerListByEqpNum.get(0).getIntoday();

                                }
                            }
                        }
                        tlFacilities.setPersoncount(percount);
                    }
                }
                tiFacilitiesVO.getFactypelist().add(tlFacilities);
            }

        }

        tiFacilitiesVO.setFactypelist(tiFacilitiesVO.getFactypelist());
        return page(tiFacilitiesTo, tiFacilitiesVO.getFactypelist());
    }


    @Override
    public List<List<Object>> getToiletPersonZX(TIFacilitiesTo tiFacilitiesTo) throws HssException, ParseException {
        List<Date> datelist = new ArrayList<>();
        List<List<Object>> inToiletPersonlist = new ArrayList<>();
        List<BTable> inToiletPerson = new ArrayList<>();
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getId())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getId, tiFacilitiesTo.getId());
        }

        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFaclname())) {
            List<String> faclnameList = Arrays.asList(tiFacilitiesTo.getFaclname().split(","));
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFaclname, faclnameList);
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacldeptid())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacldeptid, tiFacilitiesTo.getFacldeptid());
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacldeptid())) {
            List<String> depIds = departService.getChildIdsByIId(tiFacilitiesTo.getFacldeptid());
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFacldeptid, depIds);
        }
        tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, "bf83a05e10e3d47cf24d2b304c29ac98");
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);

        QueryWrapper<TlEquip> tlEquipQueryWrapper = new QueryWrapper<>();
        tlEquipQueryWrapper.lambda().eq(TlEquip::getEquiptypeid, "0affaa7feae8e0a49233b7de67cfaebd");
        List<TlEquip> tlEquipList = tlEquipMapper.selectList(tlEquipQueryWrapper);
        Map<Object, List<TlEquip>> tlEquipCollect = tlEquipList.stream().filter(item -> StringUtils.isNotEmpty(item.getBindobjid())).collect(Collectors.groupingBy(TlEquip::getBindobjid));
        for (TlFacilities tlFacilities : tlFacilitiesList) {
            if (null != tlEquipCollect.get(tlFacilities.getId())) {
                tlFacilities.setEqpnum(tlEquipCollect.get(tlFacilities.getId()).get(0).getEquipcode());
            }
        }
        Date startTime;
        Date endTime;
        String first;
        String last;
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getStartTime()) && StringUtils.isNotEmpty(tiFacilitiesTo.getEndTime())) {
            first = tiFacilitiesTo.getStartTime();
            last = tiFacilitiesTo.getEndTime();
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(tiFacilitiesTo.getEndTime()));
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(tiFacilitiesTo.getStartTime()));
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            first = format.format(c.getTime());
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(first));
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            last = format.format(ca.getTime());
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(last));

        }
        QueryWrapper<HumanCamer> HumanCamerQueryWrapper = new QueryWrapper<>();
        HumanCamerQueryWrapper.lambda().between(HumanCamer::getDeviceTime, startTime, endTime);
        HumanCamerQueryWrapper.lambda().orderByDesc(HumanCamer::getDeviceTime);
        List<HumanCamer> HumanCamerListAll = humanCamerMapper.selectList(HumanCamerQueryWrapper);
        Map<String, List<HumanCamer>> humanCamerMapByEquipNum = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
        for (HumanCamer humanCamer : HumanCamerListAll) {
            humanCamer.setDeviceDate(Tools.getYearMonthDayString(humanCamer.getDeviceTime()));
            humanCamer.setYearMon(Tools.getYearMonthString(humanCamer.getDeviceTime()));
        }
        Map<String, List<HumanCamer>> humanCamerMapByDeviceDate = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getDeviceDate));
        datelist = Tools.getBetweenDateList(first, last);
        Date s = Tools.getYearMonthDayDate(first);
        Date e = Tools.getYearMonthDayDate(last);
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFlag())) {
            if (tiFacilitiesTo.getFlag().equals("1")) {
                List<String> result = new ArrayList<String>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
                Calendar min = Calendar.getInstance();
                Calendar max = Calendar.getInstance();
                min.setTime(sdf.parse(first));
                min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
                max.setTime(sdf.parse(last));
                max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
                Calendar curr = min;
                while (curr.before(max)) {
                    result.add(sdf.format(curr.getTime()));
                    curr.add(Calendar.MONTH, 1);
                }
                List<String> datelistyue = new ArrayList<>();
                for (String s1 : result) {
                    BTable bTable = new BTable();
                    int countyue = 0;
                    datelistyue = Tools.getDayListOfMonthByDate(Tools.getYearMonthDayDate(s1 + "-01"));
                    for (String s2 : datelistyue) {
                        int count = 0;
                        for (TlFacilities tlFacilities : tlFacilitiesList) {
                            if (null == tlFacilities.getEqpnum() || null == humanCamerMapByEquipNum.get(tlFacilities.getEqpnum())) {
                                continue;
                            } else {
                                List<HumanCamer> HHumanCamerList = humanCamerMapByDeviceDate.get(s2);
                                if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                                    Map<String, List<HumanCamer>> humanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                                    List<HumanCamer> HHumanCamerListByEqpNum = humanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                                    if (null != HHumanCamerListByEqpNum && HHumanCamerListByEqpNum.size() > 0) {
                                        count = count + HHumanCamerListByEqpNum.get(0).getIntoday();
                                    }
                                }
                            }
                        }
                        countyue = countyue + count;
                    }
                    bTable.setTime(s1);
                    bTable.setData(countyue);
                    inToiletPerson.add(bTable);
                }

                List<Object> resList = new ArrayList<>();
                for (int i = 0; i < result.size(); i++) {
                    resList.add(result.get(i));
                    inToiletPersonlist.add(resList);
                    resList = new ArrayList<>();
                }
                Boolean flag = true;
                for (int i = 0; i < inToiletPersonlist.size(); i++) {
                    flag = true;
                    if (inToiletPerson == null || inToiletPerson.size() <= 0) {
                        inToiletPersonlist.get(i).add(0);
                    }
                    for (int j = 0; j < inToiletPerson.size(); j++) {
                        if (inToiletPersonlist.get(i).get(0).equals(inToiletPerson.get(j).getTime())) {
                            inToiletPersonlist.get(i).add(inToiletPerson.get(j).getData());
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        inToiletPersonlist.get(i).add(0);
                    }
                }
            }
        } else {
            for (Date date : datelist) {
                int percount = 0;
                BTable bTable = new BTable();
                for (TlFacilities tlFacilities : tlFacilitiesList) {
                    if (null == tlFacilities.getEqpnum() || null == humanCamerMapByEquipNum.get(tlFacilities.getEqpnum())) {
                        continue;
                    } else {
                        String datetian = Tools.getYearMonthDayString(date);
                        List<HumanCamer> HHumanCamerList = humanCamerMapByDeviceDate.get(datetian);
                        if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                            Map<String, List<HumanCamer>> humanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                            List<HumanCamer> HHumanCamerListByEqpNum = humanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                            if (null != HHumanCamerListByEqpNum && HHumanCamerListByEqpNum.size() > 0) {
                                percount = percount + HHumanCamerListByEqpNum.get(0).getIntoday();

                            }
                        }
                        bTable.setTime(datetian);
                        bTable.setData(percount);
                        inToiletPerson.add(bTable);
                    }

                }
            }
            inToiletPersonlist = Tools.getDateEcharts(inToiletPerson, s, e);
        }
        return inToiletPersonlist;
    }

    /**
     * 公厕人流量统计明细
     *
     * @param tiFacilitiesTo
     * @return
     * @throws HssException
     * @throws ParseException
     */
    @Override
    public ToiletPersonVO getToiletPersonMX(TIFacilitiesTo tiFacilitiesTo) throws HssException, ParseException {
        ToiletPersonVO toiletPersonVO = getToiletPersonVO(tiFacilitiesTo);
        toiletPersonVO.setToiletPersonMXlist(toiletPersonVO.getToiletPersonMXlist());
        return pageMX(tiFacilitiesTo, toiletPersonVO.getToiletPersonMXlist());
    }

    /**
     * 公厕人流量统计明细导出用
     *
     * @param tiFacilitiesTo
     * @return
     * @throws HssException
     * @throws ParseException
     */
    public List<ToiletPersonMXVO> getToiletPersonMXExcel(TIFacilitiesExcelWriteTo tiFacilitiesTo) throws HssException, ParseException {
        ToiletPersonVO toiletPersonVO = getToiletPersonVOExcel(tiFacilitiesTo);
        return toiletPersonVO.getToiletPersonMXlist();
    }

    //导出用
    private ToiletPersonVO getToiletPersonVOExcel(TIFacilitiesExcelWriteTo tiFacilitiesTo) throws ParseException {
        List<Date> datelist = new ArrayList<>();
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getId())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getId, tiFacilitiesTo.getId());

        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFaclname())) {
            List<String> faclnameList = Arrays.asList(tiFacilitiesTo.getFaclname().split(","));
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFaclname, faclnameList);
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacldeptid())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacldeptid, tiFacilitiesTo.getFacldeptid());
        }
        tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, "bf83a05e10e3d47cf24d2b304c29ac98");
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);

        QueryWrapper<TlEquip> tlEquipQueryWrapper = new QueryWrapper<>();
        tlEquipQueryWrapper.lambda().eq(TlEquip::getEquiptypeid, "0affaa7feae8e0a49233b7de67cfaebd");
        List<TlEquip> tlEquipList = tlEquipMapper.selectList(tlEquipQueryWrapper);
        Map<Object, List<TlEquip>> tlEquipCollect = tlEquipList.stream().filter(item -> StringUtils.isNotEmpty(item.getBindobjid())).collect(Collectors.groupingBy(TlEquip::getBindobjid));
        Date startTime;
        Date endTime;
        String first;
        String last;
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getStartTime()) && StringUtils.isNotEmpty(tiFacilitiesTo.getEndTime())) {
            first = tiFacilitiesTo.getStartTime();
            last = tiFacilitiesTo.getEndTime();
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(tiFacilitiesTo.getEndTime()));
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(tiFacilitiesTo.getStartTime()));
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            first = format.format(c.getTime());
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(first));
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            last = format.format(ca.getTime());
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(last));

        }

        QueryWrapper<HumanCamer> HumanCamerQueryWrapper = new QueryWrapper<>();
        HumanCamerQueryWrapper.lambda().between(HumanCamer::getDeviceTime, startTime, endTime);

        HumanCamerQueryWrapper.lambda().orderByDesc(HumanCamer::getDeviceTime);
        List<HumanCamer> HumanCamerListAll = humanCamerMapper.selectList(HumanCamerQueryWrapper);
        //获取明细日期
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(tiFacilitiesTo.getToiletpersondate())) {
            Predicate<HumanCamer> inTime = x -> (tiFacilitiesTo.getToiletpersondate().contains(Tools.getYearMonthDayString(x.getDeviceTime())));
            HumanCamerListAll = HumanCamerListAll.stream().filter(inTime).collect(Collectors.toList());
        }
        Map<String, List<HumanCamer>> humanCamerMapByEquipNum = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
        for (HumanCamer humanCamer : HumanCamerListAll) {
            humanCamer.setDeviceDate(Tools.getYearMonthDayString(humanCamer.getDeviceTime()));
        }
        Map<String, List<HumanCamer>> HumanCamerMapByDeviceDate = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getDeviceDate));

        datelist = Tools.getBetweenDateList(first, last);
        ToiletPersonVO toiletPersonVO = new ToiletPersonVO();
        for (Date date : datelist) {
            int percount = 0;
            for (TlFacilities tlFacilities : tlFacilitiesList) {

                ToiletPersonMXVO toiletPersonMXVO = new ToiletPersonMXVO();
                if (null != tlEquipCollect.get(tlFacilities.getId())) {
                    tlFacilities.setEqpnum(tlEquipCollect.get(tlFacilities.getId()).get(0).getEquipcode());
                    if (null == tlFacilities.getEqpnum() || null == humanCamerMapByEquipNum.get(tlFacilities.getEqpnum())) {
                        continue;
                    } else {
                        String datetian = Tools.getYearMonthDayString(date);
                        List<HumanCamer> HHumanCamerList = HumanCamerMapByDeviceDate.get(datetian);
                        if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                            Map<String, List<HumanCamer>> humanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                            List<HumanCamer> HumanCamerListByEqpNum = humanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                            if (null != HumanCamerListByEqpNum && HumanCamerListByEqpNum.size() > 0) {
                                percount = percount + HumanCamerListByEqpNum.get(0).getIntoday();
                                toiletPersonMXVO.setPersoncount(HumanCamerListByEqpNum.get(0).getIntoday());
                                toiletPersonMXVO.setFaclname(tlFacilities.getFaclname());
                                toiletPersonMXVO.setFaclstatus(tlFacilities.getFaclstatus());
                                toiletPersonMXVO.setToiletpersondate(datetian);
                                toiletPersonVO.getToiletPersonMXlist().add(toiletPersonMXVO);
                            }
                        }
                    }
                }
            }

        }
        return toiletPersonVO;
    }

    private ToiletPersonVO getToiletPersonVO(TIFacilitiesTo tiFacilitiesTo) throws ParseException {
        List<Date> datelist = new ArrayList<>();
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getId())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getId, tiFacilitiesTo.getId());

        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFaclname())) {
            List<String> faclnameList = Arrays.asList(tiFacilitiesTo.getFaclname().split(","));
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFaclname, faclnameList);
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacldeptid())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacldeptid, tiFacilitiesTo.getFacldeptid());
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getIdlist())) {
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getId, tiFacilitiesTo.getIdlist());
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacldeptid())) {
            List<String> depIds = departService.getChildIdsByIId(tiFacilitiesTo.getFacldeptid());
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFacldeptid, depIds);
        }
        tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, "bf83a05e10e3d47cf24d2b304c29ac98");
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);

        QueryWrapper<TlEquip> tlEquipQueryWrapper = new QueryWrapper<>();
        tlEquipQueryWrapper.lambda().eq(TlEquip::getEquiptypeid, "0affaa7feae8e0a49233b7de67cfaebd");
        List<TlEquip> tlEquipList = tlEquipMapper.selectList(tlEquipQueryWrapper);
        Map<Object, List<TlEquip>> tlEquipCollect = tlEquipList.stream().filter(item -> StringUtils.isNotEmpty(item.getBindobjid())).collect(Collectors.groupingBy(TlEquip::getBindobjid));
        Date startTime;
        Date endTime;
        String first;
        String last;
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getStartTime()) && StringUtils.isNotEmpty(tiFacilitiesTo.getEndTime())) {
            first = tiFacilitiesTo.getStartTime();
            last = tiFacilitiesTo.getEndTime();
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(tiFacilitiesTo.getEndTime()));
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(tiFacilitiesTo.getStartTime()));
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            first = format.format(c.getTime());
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(first));
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            last = format.format(ca.getTime());
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(last));

        }

        QueryWrapper<HumanCamer> HumanCamerQueryWrapper = new QueryWrapper<>();
        HumanCamerQueryWrapper.lambda().between(HumanCamer::getDeviceTime, startTime, endTime);

        HumanCamerQueryWrapper.lambda().orderByDesc(HumanCamer::getDeviceTime);
        List<HumanCamer> HumanCamerListAll = humanCamerMapper.selectList(HumanCamerQueryWrapper);
        Map<String, List<HumanCamer>> humanCamerMapByEquipNum = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
        for (HumanCamer humanCamer : HumanCamerListAll) {
            humanCamer.setDeviceDate(Tools.getYearMonthDayString(humanCamer.getDeviceTime()));
        }
        Map<String, List<HumanCamer>> HumanCamerMapByDeviceDate = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getDeviceDate));

        datelist = Tools.getBetweenDateList(first, last);
        ToiletPersonVO toiletPersonVO = new ToiletPersonVO();
        for (Date date : datelist) {
            int percount = 0;
            for (TlFacilities tlFacilities : tlFacilitiesList) {

                ToiletPersonMXVO toiletPersonMXVO = new ToiletPersonMXVO();
                if (null != tlEquipCollect.get(tlFacilities.getId())) {
                    tlFacilities.setEqpnum(tlEquipCollect.get(tlFacilities.getId()).get(0).getEquipcode());
                    if (null == tlFacilities.getEqpnum() || null == humanCamerMapByEquipNum.get(tlFacilities.getEqpnum())) {
                        continue;
                    } else {
                        String datetian = Tools.getYearMonthDayString(date);
                        List<HumanCamer> HHumanCamerList = HumanCamerMapByDeviceDate.get(datetian);
                        if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                            Map<String, List<HumanCamer>> humanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                            List<HumanCamer> HumanCamerListByEqpNum = humanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                            if (null != HumanCamerListByEqpNum && HumanCamerListByEqpNum.size() > 0) {
                                percount = percount + HumanCamerListByEqpNum.get(0).getIntoday();
                                toiletPersonMXVO.setPersoncount(HumanCamerListByEqpNum.get(0).getIntoday());
                                toiletPersonMXVO.setFaclname(tlFacilities.getFaclname());
                                toiletPersonMXVO.setFaclstatus(tlFacilities.getFaclstatus());
                                toiletPersonMXVO.setToiletpersondate(datetian);
                                toiletPersonVO.getToiletPersonMXlist().add(toiletPersonMXVO);
                            }
                        }
                    }
                }
            }

        }
        return toiletPersonVO;
    }

    private TIFacilitiesVO page(TIFacilitiesTo tiFacilitiesTo, List<TlFacilities> list) {
        // 分页
        Long page = StringUtils.isEmpty(tiFacilitiesTo.getPage()) ? 1 : tiFacilitiesTo.getPage();
        Long size = StringUtils.isEmpty(tiFacilitiesTo.getSize()) ? 10 : tiFacilitiesTo.getSize();
        TIFacilitiesVO tiFacilitiesVO = new TIFacilitiesVO();
        tiFacilitiesVO.setTotal((long) list.size());
        tiFacilitiesVO.setCurrent(page);
        tiFacilitiesVO.setSize(size);
        BigDecimal pageSizeB = new BigDecimal(size);
        BigDecimal sizeB = new BigDecimal(list.size());
        tiFacilitiesVO.setPage(Long.valueOf(sizeB.divide(pageSizeB, BigDecimal.ROUND_CEILING).toString()));
        tiFacilitiesVO.setFactypelist(new ArrayList<>());
        for (int i = 0; i < list.size(); i++) {
            if (i >= size * (page - 1) && i < size * page) {
                tiFacilitiesVO.getFactypelist().add(list.get(i));
            }
        }
        return tiFacilitiesVO;
    }

    private ToiletPersonVO pageMX(TIFacilitiesTo tiFacilitiesTo, List<ToiletPersonMXVO> list) {
        // 分页
        Long page = StringUtils.isEmpty(tiFacilitiesTo.getPage()) ? 1 : tiFacilitiesTo.getPage();
        Long size = StringUtils.isEmpty(tiFacilitiesTo.getSize()) ? 10 : tiFacilitiesTo.getSize();
        ToiletPersonVO toiletPersonVO = new ToiletPersonVO();
        toiletPersonVO.setTotal((long) list.size());
        toiletPersonVO.setCurrent(page);
        toiletPersonVO.setSize(size);
        BigDecimal pageSizeB = new BigDecimal(size);
        BigDecimal sizeB = new BigDecimal(list.size());
        toiletPersonVO.setPage(Long.valueOf(sizeB.divide(pageSizeB, BigDecimal.ROUND_CEILING).toString()));
        toiletPersonVO.setToiletPersonMXlist(new ArrayList<>());
        for (int i = 0; i < list.size(); i++) {
            if (i >= size * (page - 1) && i < size * page) {
                toiletPersonVO.getToiletPersonMXlist().add(list.get(i));
            }
        }
        return toiletPersonVO;
    }

    public List<TlFacilities> getToiletPersonExcelWrite(TIFacilitiesExcelWriteTo tiFacilitiesTo) throws HssException, ParseException {
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getId())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getId, tiFacilitiesTo.getId());

        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFaclname())) {
            List<String> faclnameList = Arrays.asList(tiFacilitiesTo.getFaclname().split(","));
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getFaclname, faclnameList);
        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getFacldeptid())) {
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacldeptid, tiFacilitiesTo.getFacldeptid());

        }
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getIdlist())) {
            tlFacilitiesQueryWrapper.lambda().in(TlFacilities::getId, tiFacilitiesTo.getIdlist());

        }
        tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid, "bf83a05e10e3d47cf24d2b304c29ac98");
        List<TlFacilities> tlFacilitiesList = tlFacilitiesMapper.selectList(tlFacilitiesQueryWrapper);
        TIFacilitiesVO tiFacilitiesVO = new TIFacilitiesVO();
        QueryWrapper<TlEquip> tlEquipQueryWrapper = new QueryWrapper<>();
        tlEquipQueryWrapper.lambda().eq(TlEquip::getEquiptypeid, "0affaa7feae8e0a49233b7de67cfaebd");
        List<TlEquip> tlEquipList = tlEquipMapper.selectList(tlEquipQueryWrapper);
        Map<Object, List<TlEquip>> tlEquipCollect = tlEquipList.stream().filter(item -> StringUtils.isNotEmpty(item.getBindobjid())).collect(Collectors.groupingBy(TlEquip::getBindobjid));

        Date startTime;
        Date endTime;
        if (StringUtils.isNotEmpty(tiFacilitiesTo.getStartTime()) && StringUtils.isNotEmpty(tiFacilitiesTo.getEndTime())) {
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(tiFacilitiesTo.getEndTime()));
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(tiFacilitiesTo.getStartTime()));
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
            String first = format.format(c.getTime());
            startTime = Tools.getDateForBegin(Tools.getYearMonthDayDate(first));
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = format.format(ca.getTime());
            endTime = Tools.getDateForEnd(Tools.getYearMonthDayDate(last));
        }
        QueryWrapper<HumanCamer> HumanCamerQueryWrapper = new QueryWrapper<>();
        HumanCamerQueryWrapper.lambda().between(HumanCamer::getDeviceTime, startTime, endTime);
        HumanCamerQueryWrapper.lambda().orderByDesc(HumanCamer::getDeviceTime);
        List<HumanCamer> HumanCamerListAll = humanCamerMapper.selectList(HumanCamerQueryWrapper);
        for (HumanCamer humanCamer : HumanCamerListAll) {
            humanCamer.setDeviceDate(Tools.getYearMonthDayString(humanCamer.getDeviceTime()));
        }

        Map<String, List<HumanCamer>> HumanCamerMapByEquipNum = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
        Map<String, List<HumanCamer>> HumanCamerMapByDeviceDate = HumanCamerListAll.stream().collect(Collectors.groupingBy(HumanCamer::getDeviceDate));

        for (TlFacilities tlFacilities : tlFacilitiesList) {

            if (null != tlEquipCollect.get(tlFacilities.getId())) {
                tlFacilities.setEqpnum(tlEquipCollect.get(tlFacilities.getId()).get(0).getEquipcode());
                if (null == tlFacilities.getEqpnum() || null == HumanCamerMapByEquipNum.get(tlFacilities.getEqpnum())) {
                    continue;
                }

                List<HumanCamer> HumanCamerList = HumanCamerMapByEquipNum.get(tlFacilities.getEqpnum());
                tlFacilities.setPersoncountall(HumanCamerList.get(0).getIntotal());

                if (!HumanCamerList.isEmpty()) {
                    if (StringUtils.isNotEmpty(tiFacilitiesTo.getStartTime()) && StringUtils.isNotEmpty(tiFacilitiesTo.getEndTime())) {
                        List<Date> datelist = new ArrayList<>();
                        int percount = 0;
                        datelist = Tools.getBetweenDateList(tiFacilitiesTo.getStartTime(), tiFacilitiesTo.getEndTime());
                        for (Date date : datelist) {

                            String datetian = Tools.getYearMonthDayString(date);
                            List<HumanCamer> HHumanCamerList = HumanCamerMapByDeviceDate.get(datetian);
                            if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                                Map<String, List<HumanCamer>> HumanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                                List<HumanCamer> HHumanCamerListByEqpNum = HumanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                                if (null != HHumanCamerListByEqpNum && HHumanCamerListByEqpNum.size() > 0) {
                                    percount = percount + HHumanCamerListByEqpNum.get(0).getIntoday();

                                }
                            }
                        }
                        tlFacilities.setPersoncount(percount);

                    } else {
                        int percount = 0;
                        List<String> datelists = Tools.getDayListOfMonth();
                        for (String date : datelists) {

                            List<HumanCamer> HHumanCamerList = HumanCamerMapByDeviceDate.get(date);
                            if (null != HHumanCamerList && HHumanCamerList.size() > 0) {
                                Map<String, List<HumanCamer>> HumanCamerMapByEqpNum = HHumanCamerList.stream().collect(Collectors.groupingBy(HumanCamer::getEqpnum));
                                List<HumanCamer> HHumanCamerListByEqpNum = HumanCamerMapByEqpNum.get(tlFacilities.getEqpnum());
                                if (null != HHumanCamerListByEqpNum && HHumanCamerListByEqpNum.size() > 0) {
                                    percount = percount + HHumanCamerListByEqpNum.get(0).getIntoday();

                                }
                            }
                        }
                        tlFacilities.setPersoncount(percount);
                    }
                }
                tiFacilitiesVO.getFactypelist().add(tlFacilities);
            }

        }

        return tiFacilitiesVO.getFactypelist();
    }


    @Override
    public List<ElementsListByAppVo> appHomeFacilities(JSONObject params) {

        List<ElementsListByAppVo> list = new ArrayList<>();

        String day = DateUtil.formatDate(new Date());
        String startTime = AttributeUtils.timeCompletion(day, 's');
        String endTime = AttributeUtils.timeCompletion(day, 'e');

        /**
         * 车辆汇总
         */
        Integer totalOnlineNumCar = 0;
        Integer totalNumCar = 0;
        List<TlCar> tlCarList = tlCarMapper.selectList(new QueryWrapper<>());
        Map<String, TlCar> tlCarCollect = tlCarList.stream().collect(Collectors.groupingBy(TlCar::getId, Collectors.collectingAndThen(Collectors.toList(), tlCar -> tlCar.get(0))));

        Map<String, List<TlCar>> tlCarCollectByType = tlCarList.stream().filter(item -> StringUtils.isNotEmpty(item.getCartypeid())).collect(Collectors.groupingBy(TlCar::getCartypeid));

        List<TlCarType> tlCarTypeList = tlCarTypeMapper.selectList(new QueryWrapper<>());
        Map<String, TlCarType> tlCarTypeCollect = tlCarTypeList.stream().collect(Collectors.groupingBy(TlCarType::getId, Collectors.collectingAndThen(Collectors.toList(), tlCarTypes -> tlCarTypes.get(0))));

        QueryWrapper<TlEquip> tlEquipQueryWrapper = new QueryWrapper<>();
        tlEquipQueryWrapper.lambda().eq(TlEquip::getOnlinestatus, "0");
        List<TlEquip> tlEquipList = tlEquipMapper.selectList(tlEquipQueryWrapper);

        List<TlEquip> tlCarEquipList = tlEquipList.stream()
                .filter(item -> StringUtils.isNotEmpty(item.getBindobjid()))
                .filter(item -> "252b781c1a454e042fe6531950f80b12".equals(item.getEquiptypeid()))
                .collect(Collectors.toList());
        // 更新设备表上的车辆类型id
        tlCarEquipList.forEach(t -> {
            t.setBindtypeitem("");
            if (null != tlCarCollect.get(t.getBindobjid())) {
                t.setBindtypeitem(tlCarCollect.get(t.getBindobjid()).getCartypeid());
            }
        });
        Map<String, List<TlEquip>> tlCarEquipCollect = tlCarEquipList.stream().collect(Collectors.groupingBy(TlEquip::getBindtypeitem));
        List<ElementsVo> carElementsVoList = new ArrayList<>();
        tlCarCollectByType.keySet().forEach(t -> {
            ElementsVo elementsVo = new ElementsVo();
            elementsVo.setCarTypeId(t);
            elementsVo.setCarType("");
            if (null != tlCarTypeCollect.get(t)) {
                elementsVo.setCarType(tlCarTypeCollect.get(t).getTypename());
                elementsVo.setCarIcon(tlCarTypeCollect.get(t).getCaricon());
            }
            carElementsVoList.add(elementsVo);
        });

        carElementsVoList.forEach(t -> {
            t.setCarNum(0);
            if (null != tlCarCollectByType.get(t.getCarTypeId())) {
                t.setCarNum(tlCarCollectByType.get(t.getCarTypeId()).size());
            }
            t.setCarOnlineNum(0);
            if (null != tlCarEquipCollect.get(t.getCarTypeId())) {
                t.setCarOnlineNum(tlCarEquipCollect.get(t.getCarTypeId()).size());
            }
        });

        for (ElementsVo elementsVo : carElementsVoList) {
            totalOnlineNumCar += null == elementsVo.getCarOnlineNum() ? 0 : elementsVo.getCarOnlineNum();
            totalNumCar += null == elementsVo.getCarNum() ? 0 : elementsVo.getCarNum();
        }
        List<TlCar> tlCarByAttendanceList = tlCarList.stream().filter(tlCar -> null != tlCar.getAttendance() && tlCar.getAttendance() == 0).collect(Collectors.toList());

        Map carMap = new HashMap();
        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        alarmCarQueryWrapper.lambda().ge(AlarmCar::getAlarmTime, startTime);
        alarmCarQueryWrapper.lambda().le(AlarmCar::getAlarmTime, endTime);
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        Map<String, Long> alarmCarMap = alarmCarList.stream().collect(Collectors.groupingBy(AlarmCar::getCarId, Collectors.counting()));
        carMap.putAll(alarmCarMap);

        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        carViolationQueryWrapper.lambda().ge(CarViolation::getTime, startTime);
        carViolationQueryWrapper.lambda().le(CarViolation::getTime, endTime);
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        Map<String, Long> carViolationMap = carViolationList.stream().collect(Collectors.groupingBy(CarViolation::getCarId, Collectors.counting()));
        carMap.putAll(carViolationMap);

        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        Set<String> carList = new HashSet<>();

//        Integer alarmCarNumber = alarmCarMap.size();
//        Integer carViolationNumber = carViolationMap.size();
        for (RptCarWork rptCarWork : rptCarWorkList) {
            carList.add(rptCarWork.getCarId());
//            List<AlarmCar> alarmCarList1 = alarmCarMap.get(rptCarWork.getCarId());
//            if (null != alarmCarList1) {
//                alarmCarNumber += alarmCarList1.size();
//            }
//            List<CarViolation> carViolationList1 = carViolationMap.get(rptCarWork.getCarId());
//            if (null != carViolationList1) {
//                carViolationNumber += carViolationList1.size();
//            }
        }
        ElementsListByAppVo elementsListByAppVo = new ElementsListByAppVo();
        elementsListByAppVo.setAttendanceNumber(tlCarByAttendanceList.size());
        Integer carAbnormalNumber = carMap.size();
        elementsListByAppVo.setAbnormalNumber(carAbnormalNumber);
        elementsListByAppVo.setWorkPlanNumber(carList.size());
        elementsListByAppVo.setTotalNum(totalNumCar);
        elementsListByAppVo.setTotalOnlineNum(totalOnlineNumCar);

        elementsListByAppVo.setNumber(1);
        elementsListByAppVo.setName("车辆");
        elementsListByAppVo.setScore(totalOnlineNumCar + "/" + totalNumCar);
        elementsListByAppVo.setRatio(new BigDecimal(String.valueOf(totalOnlineNumCar)).multiply(new BigDecimal("100")).divide(new BigDecimal(String.valueOf(totalNumCar)), 0, BigDecimal.ROUND_HALF_UP));
        list.add(elementsListByAppVo);


        /**
         * 人员汇总
         */
        Integer totalOnlineNumEmp = 0;
        Integer totalNumEmp = 0;

        List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(new QueryWrapper<>());

        Map<String, TlEmployee> stringTlEmployeeMap = tlEmployeeList.stream().collect(Collectors.groupingBy(TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), tlEmployees -> tlEmployees.get(0))));

        Map<String, List<TlEmployee>> stringListMap = tlEmployeeList.stream().filter(item -> StringUtils.isNotEmpty(item.getWorktype())).collect(Collectors.groupingBy(TlEmployee::getWorktype));

        List<TlWorkType> tlWorkTypeList = tlWorkTypeMapper.selectList(new QueryWrapper<>());
        Map<String, TlWorkType> stringTlWorkTypeMap = tlWorkTypeList.stream().collect(Collectors.groupingBy(TlWorkType::getId, Collectors.collectingAndThen(Collectors.toList(), tlWorkTypes -> tlWorkTypes.get(0))));

        List<TlEquip> tlEmpEquipList = tlEquipList.stream()
                .filter(item -> StringUtils.isNotEmpty(item.getBindobjid()))
                .filter(item -> "dc605bf7800b204e3015ac977542e66b".equals(item.getEquiptypeid()))
                .collect(Collectors.toList());

        tlEmpEquipList.forEach(t -> {
            t.setBindtypeitem("");
            if (null != stringTlEmployeeMap.get(t.getBindobjid())) {
                t.setBindtypeitem(stringTlEmployeeMap.get(t.getBindobjid()).getWorktype());
            }
        });
        Map<String, List<TlEquip>> tlEmpEquipCollect = tlEmpEquipList.stream().collect(Collectors.groupingBy(TlEquip::getBindtypeitem));

        List<ElementsVo> empElementsVoList = new ArrayList<>();
        stringListMap.keySet().forEach(t -> {
            ElementsVo elementsVo = new ElementsVo();
            elementsVo.setEmpTypeId(t);
            elementsVo.setEmpType("");
            if (null != stringTlWorkTypeMap.get(t)) {
                elementsVo.setEmpType(stringTlWorkTypeMap.get(t).getWorktype());
                elementsVo.setEmpIcon(stringTlWorkTypeMap.get(t).getEmployeeicon());
            }
            empElementsVoList.add(elementsVo);
        });

        empElementsVoList.forEach(t -> {
            t.setEmpNum(0);
            if (null != stringListMap.get(t.getEmpTypeId())) {
                t.setEmpNum(stringListMap.get(t.getEmpTypeId()).size());
            }
            t.setEmpOnlineNum(0);
            if (null != tlEmpEquipCollect.get(t.getEmpTypeId())) {
                t.setEmpOnlineNum(tlEmpEquipCollect.get(t.getEmpTypeId()).size());
            }
        });
        for (ElementsVo elementsVo : empElementsVoList) {
            totalOnlineNumEmp += null == elementsVo.getEmpOnlineNum() ? 0 : elementsVo.getEmpOnlineNum();
            totalNumEmp += null == elementsVo.getEmpNum() ? 0 : elementsVo.getEmpNum();
        }
        List<TlEmployee> tlEmployeeByAttendanceList = tlEmployeeList.stream().filter(tlEmployee -> null != tlEmployee.getAttendance() && tlEmployee.getAttendance() == 0).collect(Collectors.toList());

        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<>();
        rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getWorkDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);

        Map empMap = new HashMap();
        QueryWrapper<AlarmPerson> alarmPersonQueryWrapper = new QueryWrapper<>();
        alarmPersonQueryWrapper.lambda().ge(AlarmPerson::getAlarmTime, startTime);
        alarmPersonQueryWrapper.lambda().le(AlarmPerson::getAlarmTime, endTime);
        List<AlarmPerson> alarmPersonList = alarmPersonMapper.selectList(alarmPersonQueryWrapper);
        Map<String, List<AlarmPerson>> alarmPersonMap = alarmPersonList.stream().collect(Collectors.groupingBy(AlarmPerson::getPersonId));
        empMap.putAll(alarmPersonMap);

        QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
        dlbjViolationQueryWrapper.lambda().ge(DlbjViolation::getDate, startTime);
        dlbjViolationQueryWrapper.lambda().le(DlbjViolation::getDate, endTime);
        List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);
        Map<String, List<DlbjViolation>> dlbjViolationMap = dlbjViolationList.stream().collect(Collectors.groupingBy(DlbjViolation::getPersonId));
        empMap.putAll(dlbjViolationMap);

//        Integer alarmEmpNumber = alarmPersonMap.size();
//        Integer empViolationNumber = dlbjViolationMap.size();
        Set<String> empList = new HashSet<>();
        for (RptEmployeeWork rptEmployeeWork : rptEmployeeWorkList) {
            empList.add(rptEmployeeWork.getEmployeeId());
//            List<AlarmPerson> alarmPersonList1 = alarmPersonMap.get(rptEmployeeWork.getEmployeeId());
//            if (null != alarmPersonList1) {
//                alarmEmpNumber += alarmPersonList1.size();
//            }
//
//            List<DlbjViolation> dlbjViolationList1 = dlbjViolationMap.get(rptEmployeeWork.getEmployeeId());
//            if (null != dlbjViolationList1) {
//                empViolationNumber += dlbjViolationList1.size();
//            }
        }

        ElementsListByAppVo elementsListByAppVo1 = new ElementsListByAppVo();
        Integer empAbnormalNumber = empMap.size();
        elementsListByAppVo1.setAttendanceNumber(tlEmployeeByAttendanceList.size());
        elementsListByAppVo1.setAbnormalNumber(empAbnormalNumber);
        elementsListByAppVo1.setWorkPlanNumber(empList.size());
        elementsListByAppVo1.setTotalNum(totalNumEmp);
        elementsListByAppVo1.setTotalOnlineNum(totalOnlineNumEmp);

        elementsListByAppVo1.setNumber(2);
        elementsListByAppVo1.setName("人员");
        elementsListByAppVo1.setScore(totalOnlineNumEmp + "/" + totalNumEmp);
        elementsListByAppVo1.setRatio(new BigDecimal(String.valueOf(totalOnlineNumEmp)).multiply(new BigDecimal("100")).divide(new BigDecimal(String.valueOf(totalNumEmp)), 0, BigDecimal.ROUND_HALF_UP));
        list.add(elementsListByAppVo1);

        QueryWrapper<AlarmSheshi> alarmSheshiQueryWrapper = new QueryWrapper<>();
        alarmSheshiQueryWrapper.lambda().ge(AlarmSheshi::getAlarmTime, startTime);
        alarmSheshiQueryWrapper.lambda().le(AlarmSheshi::getAlarmTime, endTime);
        List<AlarmSheshi> alarmSheshiList = alarmSheshiMapper.selectList(alarmSheshiQueryWrapper);
        Map<String, List<AlarmSheshi>> alarmSheshiMap = alarmSheshiList.stream().filter(map -> map.getFacltypename() != null).collect(Collectors.groupingBy(AlarmSheshi::getFacltypename));
        Map<String, List<AlarmSheshi>> alarmSheshiByFacltypeitemidMap = alarmSheshiList.stream().filter(map -> map.getFacltypeitemid() != null).collect(Collectors.groupingBy(AlarmSheshi::getFacltypeitemid));

        List<TlFacilities> tlFacilities = tlFacilitiesMapper.selectList(new QueryWrapper<>());
        Map<String, List<TlFacilities>> tlFacilitiesMap = tlFacilities.stream().filter(map -> map.getFacltypename() != null).collect(Collectors.groupingBy(TlFacilities::getFacltypename));

        Map<String, List<TlFacilities>> tlFacilitiesByFacltypeitemidMap = tlFacilities.stream().filter(map -> map.getFacltypeitemid() != null).collect(Collectors.groupingBy(TlFacilities::getFacltypeitemid));
        List<TlFacilities> ljt = tlFacilitiesByFacltypeitemidMap.get("cce270d93e225dcf82df52cc7393da6b");
        Map<String, List<TlFacilities>> ljtMap = ljt.stream().collect(Collectors.groupingBy(TlFacilities::getFaclstatus));

        List<TlFacilities> normalList = ljtMap.get("0");
        Integer normalNum = Objects.nonNull(normalList) ? normalList.size() : 0;

        List<TlFacilities> repairList = ljtMap.get("2");
        Integer repairNum = Objects.nonNull(repairList) ? repairList.size() : 0;

        List<TlFacilities> suspendList = ljtMap.get("4");
        Integer suspendNum = Objects.nonNull(suspendList) ? suspendList.size() : 0;

        List<AlarmSheshi> tlFacilitiesByFacltypeitemidList = alarmSheshiByFacltypeitemidMap.get("cce270d93e225dcf82df52cc7393da6b");
        Integer abnormalNum = Objects.nonNull(tlFacilitiesByFacltypeitemidList) ? tlFacilitiesByFacltypeitemidList.size() : 0;

        ElementsListByAppVo elementsListByAppVo2 = new ElementsListByAppVo();

        elementsListByAppVo2.setNormal(normalNum);
        elementsListByAppVo2.setAbnormal(abnormalNum);
        elementsListByAppVo2.setRepair(repairNum);
        elementsListByAppVo2.setSuspend(suspendNum);

        elementsListByAppVo2.setScore(normalNum + "/" + ljt.size());
        elementsListByAppVo2.setRatio(new BigDecimal(String.valueOf(normalNum)).multiply(new BigDecimal("100")).divide(new BigDecimal(String.valueOf(ljt.size())), 0, BigDecimal.ROUND_HALF_UP));
        elementsListByAppVo2.setNumber(3);
        elementsListByAppVo2.setName("垃圾桶");
        list.add(elementsListByAppVo2);

        tlFacilitiesMap.keySet().forEach(t -> {
            List<TlFacilities> tlFacilities1 = tlFacilitiesMap.get(t);
            Map<String, List<TlFacilities>> tlFacilitiesByFaclstatusMap = tlFacilities1.stream().filter(m -> m.getFaclstatus() != null).collect(Collectors.groupingBy(TlFacilities::getFaclstatus));

            List<TlFacilities> normalList1 = tlFacilitiesByFaclstatusMap.get("0");
            Integer normalNum1 = Objects.nonNull(normalList1) ? normalList1.size() : 0;

            List<TlFacilities> repairList1 = tlFacilitiesByFaclstatusMap.get("2");
            Integer repairNum1 = Objects.nonNull(repairList1) ? repairList1.size() : 0;

            List<TlFacilities> suspendList1 = tlFacilitiesByFaclstatusMap.get("4");
            Integer suspendNum1 = Objects.nonNull(suspendList1) ? suspendList1.size() : 0;

            List<AlarmSheshi> tlFacilitiesByFacltypenameLis1 = alarmSheshiMap.get(t);
            Integer abnormalNum1 = Objects.nonNull(tlFacilitiesByFacltypenameLis1) ? tlFacilitiesByFacltypenameLis1.size() : 0;

            ElementsListByAppVo elementsListByAppVo3 = new ElementsListByAppVo();

            elementsListByAppVo3.setNormal(normalNum1);
            elementsListByAppVo3.setAbnormal(abnormalNum1);
            elementsListByAppVo3.setRepair(repairNum1);
            elementsListByAppVo3.setSuspend(suspendNum1);

            elementsListByAppVo3.setScore(normalNum1 + "/" + tlFacilities1.size());
            elementsListByAppVo3.setRatio(new BigDecimal(String.valueOf(normalNum1)).multiply(new BigDecimal("100")).divide(new BigDecimal(String.valueOf(tlFacilities1.size())), 0, BigDecimal.ROUND_HALF_UP));

            if ("公厕".equals(t)) {
                elementsListByAppVo3.setNumber(4);
                elementsListByAppVo3.setName(t);
                list.add(elementsListByAppVo3);
            } else if ("中转站".equals(t)) {
                elementsListByAppVo3.setNumber(5);
                elementsListByAppVo3.setName(t);
                list.add(elementsListByAppVo3);
            } else if ("收集点".equals(t)) {
                elementsListByAppVo3.setNumber(6);
                elementsListByAppVo3.setName(t);
                list.add(elementsListByAppVo3);
            } else if ("名木古树".equals(t)) {
                elementsListByAppVo3.setNumber(7);
                elementsListByAppVo3.setName(t);
                list.add(elementsListByAppVo3);
            } else if ("园林绿化".equals(t)) {
                elementsListByAppVo3.setNumber(8);
                elementsListByAppVo3.setName(t);
                list.add(elementsListByAppVo3);
            }
        });
        //按照设备上传定位时间正序排序
        Collections.sort(list, (o1, o2) -> {
            Integer d1 = o1.getNumber();
            Integer d2 = o2.getNumber();
            return d1.compareTo(d2);
        });
        return list;
    }

    @Override
    public List<TIFacilitiesAppTreeVO> getTreeName(JSONObject params) {
        String principal = StringUtils.isNotEmpty(params.getString("principal")) ? params.getString("principal") : "";
        String facltypeid = StringUtils.isNotEmpty(params.getString("facltypeid")) ? params.getString("facltypeid") : "";
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(principal)){
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getPrincipal,principal);
        }
        if(StringUtils.isNotEmpty(facltypeid)){
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getFacltypeid,facltypeid);
        }
        Map<String, TlFacilities> tlFacilitiesMap = tlFacilitiesService.getFacMap(tlFacilitiesQueryWrapper);
        List<TIFacilitiesAppTreeVO> tiFacilitiesAppTreeVOS = new ArrayList<>();
        if(!tlFacilitiesMap.isEmpty()){
            for(TlFacilities tlFacilities : tlFacilitiesMap.values()){
                TIFacilitiesAppTreeVO tiFacilitiesAppTreeVO = new TIFacilitiesAppTreeVO();
                tiFacilitiesAppTreeVO.setFaclid(tlFacilities.getId());
                tiFacilitiesAppTreeVO.setFaclname(tlFacilities.getFaclname());
                tiFacilitiesAppTreeVOS.add(tiFacilitiesAppTreeVO);
            }
        }
        return tiFacilitiesAppTreeVOS ;
    }

    @Override
    public List<TIFacilitiesAppIdTreeVO> getArchivesTree(JSONObject params) {
        List<TIFacilitiesAppIdTreeVO> outlist = new ArrayList<>();
        String principal = StringUtils.isNotEmpty(params.getString("principal")) ? params.getString("principal") : "";
        QueryWrapper<TlFacilities> tlFacilitiesQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(principal)){
            tlFacilitiesQueryWrapper.lambda().eq(TlFacilities::getPrincipal,principal);
        }
        Map<String, List<TlFacilities>> tlFacilitiesMap = tlFacilitiesService.getFacTypeMap(tlFacilitiesQueryWrapper);
        //设施，园林，古树判定
        if(!tlFacilitiesMap.isEmpty()){
            for (String key : tlFacilitiesMap.keySet()){
                TIFacilitiesAppIdTreeVO idtree = new TIFacilitiesAppIdTreeVO();
                String ids = "";
                if(key.equals("园林绿化") ){
                    idtree.setFacltype("园林档案");
                    for (TlFacilities tlFacilities : tlFacilitiesMap.get(key)) {
                        ids=ids+tlFacilities.getId()+",";
                    }
                } else if( key.equals("名木古树")){
                    idtree.setFacltype("古树档案");
                    for (TlFacilities tlFacilities : tlFacilitiesMap.get(key)) {
                        ids=ids+tlFacilities.getId()+",";
                    }
                } else {
                    idtree.setFacltype(key);
                    for (TlFacilities tlFacilities : tlFacilitiesMap.get(key)) {
                        ids=ids+tlFacilities.getId()+",";
                    }
                }

                idtree.setIds(ids);
                outlist.add(idtree);
            }
        }
        //车辆档案判定
        QueryWrapper<TlCar> tlCarQueryWrapper = new QueryWrapper<>();
        tlCarQueryWrapper.lambda().eq(TlCar::getBindaccount,principal);
        Map<String,TlCar> tlCarMap = tlCarService.getCarMap(tlCarQueryWrapper);
        if(!tlCarMap.isEmpty()){
            TIFacilitiesAppIdTreeVO idtree = new TIFacilitiesAppIdTreeVO();
            String ids = "";
            idtree.setFacltype("车辆档案");
            for (String key : tlCarMap.keySet()){
                ids=ids+key+",";
            }
            idtree.setIds(ids);
            outlist.add(idtree);
        }

        //人员档案判断
        QueryWrapper<TlEmployee> tlEmployeeQueryWrapper = new QueryWrapper<>();
        tlEmployeeQueryWrapper.lambda().eq(TlEmployee::getBindaccount,principal);
        Map<String,TlEmployee> tlEmployeeMap = tlEmployeeService.getEmpMap(tlEmployeeQueryWrapper);
        if(!tlEmployeeMap.isEmpty()){
            TIFacilitiesAppIdTreeVO idtree = new TIFacilitiesAppIdTreeVO();
            String ids = "";
            idtree.setFacltype("人员档案");
            for (String key : tlEmployeeMap.keySet()){
                ids=ids+key+",";
            }
            idtree.setIds(ids);
            outlist.add(idtree);
        }
        return outlist;
    }
}

